如何通过 Selenium 处理以下 Internet Explorer 弹出窗口 "Are you sure you want to leave this page?"

How to handle below Internet Explorer popup "Are you sure you want to leave this page?" through Selenium

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

您可以尝试通过 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,同样如此)

Internet Explorer 弹出窗口的文本为 您确定要离开此页面吗? 是 [=29= 的结果]WindowEventHandlers.onbeforeunload


onbeforeunload

onbeforeunload property of the WindowEventHandlers mixin is the EventHandler for processing beforeunload events. These events fire when a window is about to unload其资源。此时,文档仍然可见,事件仍然可以取消。


解决方案

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

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

You can find a relevant discussion in