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,320 points
9,866 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 6, 2020 in Selenium by Sri
• 3,190 points
16,123 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 4, 2019 in Selenium by Hardeep
5,902 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,741 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,500 points
13,776 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,190 points
9,191 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,910 points
7,267 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 3, 2020 in Selenium by Sri
• 3,190 points
7,904 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 14, 2020 in Selenium by Gitika
• 65,770 points
103,162 views
+10 votes
17 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
198,328 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP