Selenium - 无法获取子窗口的窗口句柄

时间:2014-04-16 06:33:01

标签: selenium selenium-webdriver

Selenium - 无法获取子窗口的窗口句柄 - 找到我正在使用的代码下面

wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("ADD_NEWLIST_HEAD15105-15079")));
      driver.findElement(By.id("ADD_NEWLIST_HEAD15105-15079")).click();

      String parentWindow = driver.getWindowHandle();
      Set<String> handles = driver.getWindowHandles();

      for (String windowHandle : handles) {

          if (!windowHandle.equals(parentWindow)) {         
              driver.switchTo().window(windowHandle);                 
              WebElement selectComplex = driver.findElement(By.id("WPBN_TesterID"));
              Select reqComplex = new Select(selectComplex);
              reqComplex.getOptions();
              reqComplex.selectByVisibleText(tester);

              wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='WPBN_TestingEffort']")));
              driver.findElement(By.xpath(".//*[@id='WPBN_TestingEffort']")).clear();
              driver.findElement(By.xpath(".//*[@id='WPBN_TestingEffort']")).sendKeys(Tduration);

              driver.findElement(By.id("SAVEUI_HEAD15105-15079")).click();
              driver.switchTo().window(parentWindow);
          }
      }

1 个答案:

答案 0 :(得分:0)

点击链接后需要等待新窗口加载。使用以下代码:

wait.until(ExpectedConditions.visibilityOfElementLocated(By
    .id("ADD_NEWLIST_HEAD15105-15079")));
int prevWndCount = driver.getWindowHandles().size();
driver.findElement(By.id("ADD_NEWLIST_HEAD15105-15079")).click();

String parentWindow = driver.getWindowHandle();

final int currWndCount = prevWndCount;

try {

    // Waits for 60 seconds
    WebDriverWait wait = new WebDriverWait(driver, 60);

    // Wait until expected condition (Window count increases) met
    wait.until((ExpectedCondition<Boolean>) new ExpectedCondition<Boolean>() {

    public Boolean apply(WebDriver d) {

        // Return true if window count increases, else return false
        return d.getWindowHandles().size() > currWndCount;

    }

    });

}

catch (Throwable waitForNewWindowException) {
    System.out
        .println("Exception while waiting for new window to appear : "
            + waitForNewWindowException.getMessage());
}

Set<String> handles = driver.getWindowHandles();

for (String windowHandle : handles) {

    if (!windowHandle.equals(parentWindow)) {
    driver.switchTo().window(windowHandle);
    WebElement selectComplex = driver.findElement(By
        .id("WPBN_TesterID"));
    Select reqComplex = new Select(selectComplex);
    reqComplex.getOptions();
    reqComplex.selectByVisibleText(tester);

    wait.until(ExpectedConditions.visibilityOfElementLocated(By
        .xpath(".//*[@id='WPBN_TestingEffort']")));
    driver.findElement(By.xpath(".//*[@id='WPBN_TestingEffort']"))
        .clear();
    driver.findElement(By.xpath(".//*[@id='WPBN_TestingEffort']"))
        .sendKeys(Tduration);

    driver.findElement(By.id("SAVEUI_HEAD15105-15079")).click();
    driver.switchTo().window(parentWindow);
    }
}