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

3

2

How to Handle below IE Popup in Selenium

enter image description here

Supplement answered 22/3, 2019 at 5:28 Comment(1)
you can use geeksforgeeks.org/robot-class-java-awt robot class for handling this popup - Refer that link for more information.Mur
S
2

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?

Sistrunk answered 22/3, 2019 at 7:58 Comment(0)
F
0

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)

Franctireur answered 22/3, 2019 at 6:1 Comment(2)
Can you please give me solution for this #55179689Supplement
It would be the same thing right? Just switch to the alert and accept it. I can add an answer on there if you like.Franctireur
B
0

For java, you can usee the following code,

    // Set the path to the ChromeDriver executable
    System.setProperty("webdriver.chrome.driver", "path_to_chromedriver");

    // Create ChromeOptions instance
    ChromeOptions options = new ChromeOptions();

    // Disable pop-ups
    options.addArguments("--disable-popup-blocking");

    // Create WebDriver instance
    WebDriver driver = new ChromeDriver(options);

    // Rest of your code...

Hope this helps. Thanks.

Basting answered 3/7, 2023 at 0:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.