如何通过Selenium处理以下Internet Explorer弹出窗口“你确定要离开这个页面吗?”

问题描述 投票:1回答:2

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

javascript java selenium internet-explorer onbeforeunload
2个回答
0
投票

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


onbeforeunload

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


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

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

0
投票

您可以尝试通过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,则再次)

© www.soinside.com 2019 - 2024. All rights reserved.