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

一曲冷凌霜 提交于 2019-12-18 09:24:51

问题


How to Handle below IE Popup in Selenium


回答1:


The Internet Explorer popup with text as Are you sure you want to leave this page? is the result of WindowEventHandlers.onbeforeunload


onbeforeunload

The onbeforeunload property of the WindowEventHandlers mixin is the EventHandler for processing beforeunload events. These events fire when a window is about to unload its resources. At this point, the document is still visible and the event is still cancelable.


Solution

There are different strategies available to handle this popup. However, as a Cross Browser solution, you can disable this dialog invoking the executeScript() to set window.onbeforeunload as function() {}; and you can use the following solution:

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

You can find a relevant discussion in How to disable a “Reload site? Changes you made may not be saved” popup for (python) selenium tests in chrome?




回答2:


You can try to accept the alert via selenium. Not sure what language you're using but the following Java method should accept the alert and let you move on with your life.

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
    }
}

You'll want to add import org.openqa.selenium.Alert; to your imports (again if you're using Java)



来源:https://stackoverflow.com/questions/55293394/how-to-handle-below-internet-explorer-popup-are-you-sure-you-want-to-leave-this

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!