【问题标题】:How to handle below Internet Explorer popup "Are you sure you want to leave this page?" through Selenium如何处理 Internet Explorer 弹出窗口“您确定要离开此页面吗?”通过硒
【发布时间】:2019-08-13 01:45:17
【问题描述】:

如何在 Selenium 中处理下面的 IE 弹出窗口

【问题讨论】:

标签: javascript java selenium internet-explorer onbeforeunload


【解决方案1】:

您可以尝试通过 selenium 接受警报。不确定您使用的是哪种语言,但以下 Java 方法应该会接受警报并让您继续您的生活。

public void checkAlert() 
{
    try 
    {
        // Wait for the alert to show
        WebDriverWait wait = new WebDriverWait(driver, 2);
        wait.until(ExpectedConditions.alertIsPresent());

        driver.switchTo().alert().accept();

    } 
    catch (Exception e) 
    {
        //exception handling
    }
}

您需要将 import org.openqa.selenium.Alert; 添加到您的导入中(如果您使用 Java,请再次添加)

【讨论】:

【解决方案2】:

Internet Explorer 弹出窗口的文本为 您确定要离开此页面吗?WindowEventHandlers.onbeforeunload 的结果 p>


onbeforeunload

WindowEventHandlers mixin 的onbeforeunload 属性是用于处理beforeunload 事件的EventHandler。这些事件在窗口即将使用unload 其资源时触发。此时,文档仍然可见,事件仍然可以取消。


解决方案

有不同的策略可用于处理此弹出窗口。但是,作为跨浏览器解决方案,您可以禁用此对话框,调用executeScript()window.onbeforeunload 设置为function() {};,您可以使用以下解决方案:

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

您可以在How to disable a “Reload site? Changes you made may not be saved” popup for (python) selenium tests in chrome?找到相关讨论

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-08
  • 2011-10-12
  • 1970-01-01
  • 2016-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多