标签:获取 alert boolean expected 原因 rri turn wait oal
1.对于可能因某些原因延迟出现的浏览器Alert弹窗的点击操作
public void waitAlertClick(){
WebDriverWait wait = new WebDriverWait(driver, 10);//最长10s
try {
Alert alert = wait.until(new ExpectedCondition<Alert>() {
@Override
public Alert apply(WebDriver driver1) {
try {
return driver.switchTo().alert(); //获取Alert对象
} catch (NoAlertPresentException | UnhandledAlertException ignored) { //异常可根据实际情况捕获处理
return null;
}
}
});
alert.accept(); //这里点击确定,也可做其他操作
} catch (NullPointerException e) {
System.out.println("No Alert");//等待超时,未出现弹窗,alert为null则执行此处
}
2.对于有隐藏属性的元素的显示状态判断,以此等待页面加载元素可见后,进行下一步操作
public void waitEleIsDisplay(By by){ //等待元素可见,最多15s。
WebDriverWait wait = new WebDriverWait(driver, 15);
try {
wait.until(new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver1) {
try {
WebElement ele=driver.findElement(by);
return ele.isDisplayed();
}catch (NoSuchElementException e){
return null;
}
}
});
}catch (TimeoutException e ){
System.out.println("等待元素显示超时Timed out after 10 seconds waiting for:"+by);
}
}
标签:获取 alert boolean expected 原因 rri turn wait oal
原文地址:https://www.cnblogs.com/HJkoma/p/9240058.html