简体   繁体   English

在网页未完全加载时,如何强制WebDriver执行命令?

[英]How to force WebDriver to execute a command while the webpage is not loaded completely?

Webpage loads from top to bottom. 网页从上到下加载。

1) <html ...
2) <head ...
3) <body ...
  etc

I need my WebDriver explicitly wait until the <title tag is visible. 我需要WebDriver明确等待,直到<title标记可见。 Then read the content of the title, and continue doing other actions without waiting for the whole page being loaded!!! 然后阅读标题的内容,并继续执行其他操作, 而无需等待整个页面被加载!!! .

WebDriver driver = new ChromeDriver();
driver.get("http://");
WebDriverWait wait = new WebDriverWait(driver, 0) // This line is probably the one to be transformed in order to correspond to my requirements
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//title")));
// The problem is that the following commands do not start off until the whole page is loaded
if(driver.getTitle().equals("whatever")) {
driver.get("http://");
}
else {
...
}

You could use pageLoadTimeout . 您可以使用pageLoadTimeout Set the timeout to 0 and then catch the TimeoutException and then do your specific Title asserts etc. 将超时设置为0 ,然后捕获TimeoutException ,然后执行特定的Title断言等。

driver.manage().timeouts().pageLoadTimeout(0, TimeUnit.SECONDS);

UPDATE UPDATE

driver.manage().timeouts().pageLoadTimeout(0, TimeUnit.SECONDS);
WebDriverWait wait = new WebDriverWait(driver, 30);
try {
    driver.get("http://");
} catch (TimeoutException e) {
    wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//title")));
}

For more info 欲了解更多信息

Ideally you should use ExpectedConditions.titleIs() More info for the available options, can be found here . 理想情况下,您应该使用ExpectedConditions.titleIs()有关可用选项的更多信息,可在此处找到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM