Selenium webdriver 实施';waitForNewWindow';在SeleniumWebDriver中

Selenium webdriver 实施';waitForNewWindow';在SeleniumWebDriver中,selenium-webdriver,Selenium Webdriver,我正在尝试使用SeleniumWebDriver在Java中实现一个方法“waitForNewWindow”。这个方法就是等待检查是否打开了一个新窗口。如果在指定的时间内打开了一个新窗口,我需要返回true,否则返回false 公共布尔waitForNewWindow(字符串目标){ 但在这里,我不想使用thread.sleep(time),等待时间需要指定如下: try { ExpectedCondition<Boolean> e = new ExpectedCond

我正在尝试使用SeleniumWebDriver在Java中实现一个方法“waitForNewWindow”。这个方法就是等待检查是否打开了一个新窗口。如果在指定的时间内打开了一个新窗口,我需要返回true,否则返回false

公共布尔waitForNewWindow(字符串目标){

但在这里,我不想使用thread.sleep(time),等待时间需要指定如下:

   try {
    ExpectedCondition<Boolean> e = new ExpectedCondition<Boolean>() {
      public Boolean apply(WebDriver wd) {
        if (wd.switchTo().window(newTarget) != null) {
            log.info("New window is opened with the window id : "
                            + newTarget);
            driver.switchTo().window(parentHandle);
            return true;
        } else {
            return false;
        }

      }
    };
    WebDriverWait wait = new WebDriverWait(driver, TIMEOUT);

       if (wait.until(e)) {
        log.info("the wait for the expected condition is successful");
                    return true;
       }

   } catch (Exception e1) {
    log.debug(e1);
    return false;
   }
WebDriverWait wait=新的WebDriverWait(驱动程序,超时)


此外,在上面的代码中,控件正在切换到新窗口,这不是预期的。是否有人可以提供有关如何实现我的要求的答案?

您不能指定所需的超时。您必须使用Thread.sleep()

关于您的控件由于下线而移动到新窗口,控件将移动到新选项卡

driver.switchTo().window(target)
如果您想简单地检查是否有两个窗口打开,您可以编写如下内容

while( driver.getWindowHandle().length() != 2){
    Thread.sleep(2000);
}

下面提到的代码检查超时时出现的窗口数

public void waitForNumberOfWindows(final int length){
    new WebDriverWait(driver, 30) {
    }.until(new ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver driver) {
            return driver.getWindowHandle().length()==length;
        }
    });
}
public void waitForNumberOfWindows(最终整数长度){
新WebDriverWait(驱动程序,30){
}.直到(新的预期条件){
公共布尔应用(WebDriver驱动程序){
返回驱动程序。getWindowHandle().length()==长度;
}
});
}

它将检查该实例中出现的预期窗口数,如果计数与指定的超时(上述代码中的30)匹配,则返回true。

最终使用WebDriverWait对象实现waitForNewWindow方法,如下所示:

   try {
    ExpectedCondition<Boolean> e = new ExpectedCondition<Boolean>() {
      public Boolean apply(WebDriver wd) {
        if (wd.switchTo().window(newTarget) != null) {
            log.info("New window is opened with the window id : "
                            + newTarget);
            driver.switchTo().window(parentHandle);
            return true;
        } else {
            return false;
        }

      }
    };
    WebDriverWait wait = new WebDriverWait(driver, TIMEOUT);

       if (wait.until(e)) {
        log.info("the wait for the expected condition is successful");
                    return true;
       }

   } catch (Exception e1) {
    log.debug(e1);
    return false;
   }
试试看{
ExpectedCondition e=新的ExpectedCondition(){
公共布尔应用(WebDriver wd){
if(wd.switchTo().window(newTarget)!=null){
log.info(“使用窗口id打开新窗口:”
+新目标);
driver.switchTo().window(parentHandle);
返回true;
}否则{
返回false;
}
}
};
WebDriverWait wait=新的WebDriverWait(驱动程序,超时);
如果(等到(e)){
log.info(“等待预期条件成功”);
返回true;
}
}捕获(异常e1){
log.debug(e1);
返回false;
}

测试了相同的功能,并且工作正常。

只是一个想法,而不是将驱动程序的控制权转移到一个新选项卡上,然后在该选项卡上检查null。您可以执行driver.getWindowHandle().length()>1。如果可以,这将给您带来一些良好的吞吐量。我尝试使用driver.getWindowHandle().length(),但不确定其返回值为“38”的原因。对于语句“log.info”(“当前窗口数的值为:”+wd.getWindowHandle().length());”,我得到的结果是“当前窗口数的值为:38”。很抱歉,它将是:driver.getWindowHandles().size().如果你有两个窗口,它应该返回两个。请你试一试好吗?