【问题标题】:How to wait for an alert in Selenium webdriver ? [duplicate]如何在 Selenium webdriver 中等待警报? [复制]
【发布时间】:2012-09-28 11:37:45
【问题描述】:

可能重复:
selenium 2.4.0, how to check for presence of an alert

我正在使用以下代码来关闭警报窗口:

Alert alert3 = driver.switchTo().alert();
alert3.dismiss();

警报会在主窗口打开几秒钟后出现。

如何等待并检查是否出现警报?

【问题讨论】:

标签: java selenium alert selenium-webdriver


【解决方案1】:

没有等待警报的默认方法。

但是,您可以像这样编写自己的方法。

waitForAlert(WebDriver driver)
{
   int i=0;
   while(i++<5)
   {
        try
        {
            Alert alert = driver.switchTo().alert();
            break;
        }
        catch(NoAlertPresentException e)
        {
          Thread.sleep(1000);
          continue;
        }
   }
}

【讨论】:

  • 客户端不接受我们正在使用死等待(Thread.sleep),他根本不会接受 PR。我们不能用预期的条件替换它吗?
【解决方案2】:
public boolean isAlertPresent() {

  boolean presentFlag = false;

  try {

   // Check the presence of alert
   Alert alert = driver.switchTo().alert();
   // Alert present; set the flag
   presentFlag = true;
   // if present consume the alert
   alert.accept();

  } catch (NoAlertPresentException ex) {
   // Alert not present
   ex.printStackTrace();
  }

  return presentFlag;

 }

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-31
  • 2019-07-25
  • 2014-04-04
  • 1970-01-01
  • 1970-01-01
  • 2017-06-07
  • 2019-01-31
相关资源
最近更新 更多