Mie*_*Yan 3 java selenium-chromedriver selenium-webdriver nosuchelementexception fluentwait
我知道,在等待DOM尚未出现的网络元素方面,最有效的是流畅的等待.所以我的问题是:
有没有办法处理和捕获NoSuchElementException流利的等待可能抛出的或任何异常因为元素不存在?
我需要一个布尔方法,它会给我结果是否找到元素.
这种方法在网上很流行.
public void waitForElement(WebDriver driver, final By locator){
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(60, TimeUnit.SECONDS)
.pollingEvery(2, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return driver.findElement(locator);
}
});
}
Run Code Online (Sandbox Code Playgroud)
我需要的是,**.ignoring(NoSuchElementException.class);**不会被忽视.一旦捕获到异常,它将返回FALSE.另一方面,当找到元素时,它将返回TRUE.
作为替代方法,您希望看到WebDriverWait使用轮询的实现,这里是构造函数的详细信息:
WebDriverWait(WebDriver driver, long timeOutInSeconds) :Wait将忽略在'until'条件中默认遇到(抛出)的NotFoundException实例,并立即传播所有其他实例.
WebDriverWait wait1 = new WebDriverWait(driver, 10);
Run Code Online (Sandbox Code Playgroud)WebDriverWait(WebDriver driver, long timeOutInSeconds, long sleepInMillis) :Wait将忽略在'until'条件中默认遇到(抛出)的NotFoundException实例,并立即传播所有其他实例.
WebDriverWait wait2 = new WebDriverWait(driver, 10, 500);
Run Code Online (Sandbox Code Playgroud)为了回答你的评论,我们刚刚在WebDriverWait这里定义了实例.接下来,我们必须通过适当的子句在代码中实现WebDriverWaitie wait1/ .该对在这里.wait2ExpectedConditionJavaDocsExpectedCondition
| 归档时间: |
|
| 查看次数: |
1301 次 |
| 最近记录: |