为什么我的代码无法正常工作?

时间:2016-08-30 12:04:11

标签: java selenium testing

这是我的代码:

try {
     WebDriverWait wait = new WebDriverWait(driver, 10);
     Alert alert = wait.until(ExpectedConditions.alertIsPresent());

      //Accepting alert.
      alert.accept();
      System.out.println("Accepted the alert successfully.");
   } catch(Throwable e) {
      System.err.println("Error came while waiting for the alert popup. "+e.getMessage());
   }

错误显示如下:

  

等待警报弹出窗口时出错。预期条件失败:等待警报出现(尝试10秒钟,间隔500 MILLISECONDS)

2 个答案:

答案 0 :(得分:2)

我建议使用以下代码。

WebDriverWait wait = new WebDriverWait(driver, 2);
wait.until(ExpectedConditions.alertIsPresent());
try{
    Alert alert = driver.switchTo().alert().accept();
}
catch (Exception e)
    System.out.println("No alert");

答案 1 :(得分:1)

通常没有必要等待警报。您可以使用正常的代码,如

  Alert alert = webDriver.switchTo().alert();

试一试。 PS:这个Alert类只能通过javascript处理警告框,检查你的警报是否不是由任何html元素构成

相关问题