【问题标题】:WebDriver dismiss an Alert boxWebDriver 关闭警报框
【发布时间】:2010-08-31 22:07:16
【问题描述】:

如何通过单击“确定”按钮关闭警报框?我在 WebDriver Google 群组上看到 Simon 正在添加此功能。我不确定这是否受支持。

【问题讨论】:

    标签: webdriver


    【解决方案1】:

    C# 代码:

    IAlert alert = driver.SwitchTo().Alert();
    alert.Accept(); 
    System.Threading.Thread.Sleep(milliseconds);
    

    【讨论】:

      【解决方案2】:
      driver.findElement(By.id("updateButton")).click();
      

      //pop up with id "updateButton" opens

      Alert alert = driver.switchTo().alert();
      //update is executed
      alert.accept();
      

      【讨论】:

      • 这是正确的做法,尤其是在 2012 年左右之后。我不明白为什么不将其作为问题的答案进行检查,因为它应该是。
      【解决方案3】:

      看看issue 27 in the Google Code Issues List。使用 JavascriptExecutor 覆盖浏览器的警报:

      ((JavascriptExecutor)driver).executeScript("window.alert = function(msg){};");
      

      这是处理确认对话框的类似解决方案。

      ((JavascriptExecutor)driver).executeScript("window.confirm = function(msg){return true;};");
      

      这是一个非常受欢迎的请求,因此希望它会尽快实施。这些方法的缺点是验证消息中的文本并不容易。您可能希望降级到 Selenium 来执行此操作。

      【讨论】:

      • 我试过((JavascriptExecutor)driver).executeScript("window.confirm = function(msg){return true;};");,但没有运气。似乎没有人有可靠的解决方法。我真的希望他们能尽快完成这项工作。还是谢谢。
      • 再次检查您的驱动程序是否引用了正确的页面/框架。我已经成功运行了它,所以它必须是您的页面独有的东西,而不是代码。
      【解决方案4】:

      Python 代码:

      driver.switch_to_alert().accept()
      

      见:

      站点包/selenium/webdriver/remote/webdriver.py

      站点包/selenium/webdriver/common/alert.py

      【讨论】:

        猜你喜欢
        • 2010-10-02
        • 1970-01-01
        • 2014-09-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-11
        • 1970-01-01
        相关资源
        最近更新 更多