[英]How to force WebDriver to execute a command while the webpage is not loaded completely?
网页从上到下加载。
1) <html ...
2) <head ...
3) <body ...
etc
我需要WebDriver明确等待,直到<title
标记可见。 然后阅读标题的内容,并继续执行其他操作, 而无需等待整个页面被加载!!! 。
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 {
...
}
您可以使用pageLoadTimeout
。 将超时设置为0
,然后捕获TimeoutException
,然后执行特定的Title断言等。
driver.manage().timeouts().pageLoadTimeout(0, TimeUnit.SECONDS);
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")));
}
欲了解更多信息
理想情况下,您应该使用ExpectedConditions.titleIs()
有关可用选项的更多信息,可在此处找到。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.