代码之家  ›  专栏  ›  技术社区  ›  Milan Vidakovic

Selenium for C中的警报阻止执行#

  •  0
  • Milan Vidakovic  · 技术社区  · 7 年前

    我在使用硒元素时遇到了一个问题,这似乎没有什么意义。

    • Form
    • 通过单击按钮提交表单
    • 出现带有确认ID的警报弹出窗口

    然而,在进行硒测试时, Alert driver.SwitchTo().Alert() ,用于访问和关闭弹出窗口。

    如果有区别,提交按钮位于 iframe .

    chrome not reachable

    使用最新版本的Selenium Chrome驱动程序(141.0和141.2)

    编辑:

    iframe SwitchTo() iframe 显示警报后,正在阻塞。因此,您既不能切换回 DefaultContext Alert().Dismiss()

    public void SubmitForm ()
    {
        _driver.SwitchTo().Frame(_driver.FindElement(By.Id("iframe1")));
        _driver.FindElement(By.Id("submitButton1")).Click();
    
        // Alert appears        
    
        // Just an extension method that tries `_driver.SwitchTo().Alert()`
        _driverWait.Until(c => c.AlertDisplayed()); // Blocked by alert
    
        _driver.SwitchTo().Alert().Accept(); // Not reached
    }
    

    在Firefox驱动程序上,工作正常

    1 回复  |  直到 7 年前
        1
  •  3
  •   Recep Duman    7 年前

    如果需要跳过警报,可以使用下面的代码禁用通知;

     ChromeOptions options = new ChromeOptions();
     options.AddArguments("--disable-notifications"); // to disable notification
     driver = new ChromeDriver(options);
    

    options.UnhandledPromptBehavior = UnhandledPromptBehavior.Dismiss; //.accept, .dismiss, etc.
    

    编辑:

    您还可以尝试以下代码吗? DotNetSeleniumExtras.WaitHelpers 从nuget打包并将该命名空间导入到类中。

    发件人:

    _driverWait.Until(c => c.AlertDisplayed()); // Blocked by alert
    

    _driverWait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.AlertIsPresent());