繁体   English   中英

Selenium Webdriver明确等待警报引发UnhandledAlertException

[英]Selenium webdriver explicit wait for alert throws UnhandledAlertException

我必须明确等待20秒才能收到警报。 如果20秒后没有警报,我应该抛出异常。 以下是我的警报等待,但它在20秒前引发了未处理的警报异常。 有人可以帮我吗?

try {
    new WebDriverWait(driver, 20).ignoring(NoAlertPresentException.class)
            .ignoring(UnhandledAlertException.class) 
            .until(ExpectedConditions.alertIsPresent());

} catch (Exception e) {
}

编写自己的ExpectedConditions类怎么样?

public abstract class MyExpectedConditions {
public static ExpectedCondition<Boolean> waitForAlert() {
    return new ExpectedCondition<Boolean>() {

        @Override
        public Boolean apply(WebDriver driver) {
            try {
                driver.switchTo().alert();
                driver.switchTo().defaultContent();
                return true;
            } catch (NoAlertPresentException e) {
                return false;
            }
        }
    };
}

}

用法:

WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(MyExpectedConditions.waitForAlert());

说明:WebDriverWait导致开关在20秒内发出警报。 如果警报不存在, Exception被抛出的捕获。 如果driver成功切换到alert ,它将返回defaultContent并继续您的代码。

如果要处理此警报,则必须自己切换为警报。

暂无
暂无

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

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