Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 等待selenium webdriver,直到加载完成_Java_Selenium_Automation_Selenium Webdriver_Testng - Fatal编程技术网

Java 等待selenium webdriver,直到加载完成

Java 等待selenium webdriver,直到加载完成,java,selenium,automation,selenium-webdriver,testng,Java,Selenium,Automation,Selenium Webdriver,Testng,我在eclipse中使用selenium webdriver和TestNG。问题是某些数据的页面在中途重新加载,而此重新加载的时间是灵活的,这就是为什么我无法应用显式等待时间。我想让webdriver等待,直到此重新加载完成 我正试图通过这段代码做到这一点…但它不起作用 public void waitForPageLoadingToComplete() throws Exception { ExpectedCondition<Boolean> expectation

我在eclipse中使用selenium webdriver和TestNG。问题是某些数据的页面在中途重新加载,而此重新加载的时间是灵活的,这就是为什么我无法应用显式等待时间。我想让webdriver等待,直到此重新加载完成

我正试图通过这段代码做到这一点…但它不起作用

public void waitForPageLoadingToComplete() throws Exception {
        ExpectedCondition<Boolean> expectation = new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver driver) {
                return ((JavascriptExecutor) driver).executeScript(
                        "return      document.readyState").equals("complete");
            }
        };
        Wait<WebDriver> wait = new WebDriverWait(driver, 30);
        wait.until(expectation);
    }
public void waitForPageLoadingToComplete()引发异常{
ExpectedCondition expectation=新的ExpectedCondition(){
公共布尔应用(WebDriver驱动程序){
返回((JavascriptExecutor)驱动程序).executeScript(
“return document.readyState”)。等于(“完成”);
}
};
Wait Wait=newwebdriverwait(驱动程序,30);
等待,直到(期望);
}

您应该使用
WebDriverWait
并将超时设置为您可以等待的最大时间。一旦发现页面加载了所需的数据(例如,通过检查某个元素的可见性),就可以继续测试用例


请参阅中的示例。

尝试以下代码来处理页面加载/页面刷新超时

WebDriver driver = new FireFoxDriver();
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);

请使用最新版本的chrome驱动程序,因为旧版本的chrome驱动程序不处理页面等待。

无限期等待不是一个好主意。网站的时间安排也是测试的一部分。如果可能,找出您正在测试的“页面”的服务级别协议。如果没有,请运行网站速度测试(这里有一个测试方法:),并使用平均时间。如果这也不起作用,最后一个选择是使用行业标准

document.readyState()不反映正确的页面加载时间(例如,它不等待图像/脚本完全加载)。建议并测试等待页面上的元素(最好是您将在下一步测试中操作的元素)的选项。正如其他人所建议的,使用WebDriverWait时,使用“Visibility of”、“Presence of Element”等预期条件方法或多种方法,这样应该很好。

重新加载时间从2秒到40秒不等,所以我不能总是将等待时间设置为40秒以上,我想让它在加载完成后立即等待。
WebDriverWait
;它只等待满足所需条件所需的时间,然后返回
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);