Selenium NoAlertPresentException

Mat*_*tim 5 selenium selenium-webdriver

我正在尝试使用selenium WebDriver处理对话框(Ok Cancel type).所以我的目标是点击"确定"按钮.

场景是:

  1. 单击按钮以调用对话框

    button.click();

  2. 试着接受

    webDriver.switchTo().alert().accept();

但我总是得到NoAlertPresentException并且看到对话框几乎立即关闭.在我看来,Selenium自动关闭对话框,当我想接受时,没有什么可以接受的.

我很抱歉我的英语不好.

Pet*_*ček 6

此问题的常见原因是Selenium太快并且尝试接受浏览器尚未打开的警报.这可以通过显式等待简单地修复:

button.click();
WebDriverWait wait = new WebDriverWait(driver, 5);
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
alert.accept();
Run Code Online (Sandbox Code Playgroud)