How to close browser popup in Selenium WebDriver

0 votes
I am automating a web App using selenium web driver and when I want to close the browser I get a popup asking "Are you sure? The page is asking you to confirm that you want to leave - data entered will be lost." It has two buttons: Leave Page and Stay on Page

How to click on this popup button?
May 10, 2018 in Selenium by Martin
• 4,340 points
6,294 views

2 answers to this question.

0 votes

You can do this using the below code: 

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

answered May 10, 2018 by Meci Matt
• 9,460 points
0 votes
driver.quit()
answered Jun 14, 2019 by anonymous
Hey! I tried this. It doesn't work. When I run this, the whole browser is getting closed. I just want to handle the pop-up. Any other solution?

Hey Vasuki, you can simply handle the pop-up by using following code snippet: 

// Accepts (Click on OK) on Alert Pop-up window

Alert alertOK = driver.switchTo().alert();
alertOK.accept();

//Rejects (Click on Cancel) on Alert pop-up window.

Alert alertCancel = driver.switchTo().alert();
alertCancel.dismiss();

Tried with below snippet of code, it is failing. Can you please help me ?

    @Test(priority=3)

    public void display() throws InterruptedException, AWTException {

        ( ( JavascriptExecutor )driver )

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

    driver.close();

Alert alertCancel = driver.switchTo().alert();

alertCancel.dismiss();

        Thread.sleep(14000);

        System.out.println("==Executted==");

    }

@Mahesh, is it throwing any error or simply not executing the code? Also, why are you using javascript executor?

Related Questions In Selenium

0 votes
2 answers

How to open a browser window in full screen using Selenium WebDriver with C#

Hi , we have inbuilt method Maximize(). driver.Manage().Wind ...READ MORE

answered Sep 5, 2020 in Selenium by Sri
• 3,050 points
12,076 views
0 votes
1 answer
0 votes
1 answer

How to disable infobar in Chrome browser in Selenium Webdriver?

Hi Lerry, you can disable Infobars displayed ...READ MORE

answered Jun 3, 2019 in Selenium by Hardeep
3,426 views
0 votes
0 answers

How to handle browser popup in Selenium using Java?

Generally, we scrap the data from websites ...READ MORE

Jun 7, 2019 in Selenium by Vaishnavi
• 1,180 points
1,021 views
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,520 points
5,334 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,050 points
6,015 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,890 points
3,724 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 2, 2020 in Selenium by Sri
• 3,050 points
4,533 views
0 votes
8 answers

How to open a link in new tab of chrome browser using Selenium WebDriver?

This below code works for me in ...READ MORE

answered Dec 13, 2020 in Selenium by Gitika
• 65,910 points
70,215 views
+11 votes
16 answers

How to automate gmail login process using selenium webdriver in java?

Check the below code: Here is the working ...READ MORE

answered Apr 24, 2018 in Selenium by Vardy
• 2,360 points
141,292 views