可以使用配置文件设置默认超时时间
也可以直接指定超时时间,获取元素是否存在
可以用于等待页面加载
/**
* 判断元素是否存在,等待到超时时间,最长超时时间在配置文件中定义,元素需要可以显示
* @param locator
* @param timeout
* @return 元素是否存在的boolean值
* */
//(1/2)
public boolean isElementPresent(By locator) {
return isElementPresent(locator,timeout);
}
//可以指定超时时间的
//(2/2)
private boolean isElementPresent(final By locator, float timeout) {
// TODO Auto-generated method stub
boolean isPresent = false;
WebDriverWait wait = new WebDriverWait(driver,(int)timeout);
try {
isPresent = wait.until(new ExpectedCondition<Boolean>(){
@Override
public Boolean apply(WebDriver d) {
try{
return d.findElement(locator).isDisplayed();
} catch (NoSuchElementException ex) {
return false;
}
}
}
);
} catch (TimeoutException e) {
LogRecorder.Error("查找可视元素 " + locator.toString() + " 超时.");
}
return isPresent;
}