Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium WebDriver how to close browser popup

I am writing tests for a web App using selenium webDriver and came across a scenario where when I try to close the browser I get a popup saying "Are you sure? The page is asking you to confirm that you want to leave - data entered will be lost." with 2 buttons: Leave Page and Stay on Page

How do I click on those buttons?

like image 904
Maalamaal Avatar asked Jul 28 '11 00:07

Maalamaal


People also ask

How do I close a popup window in Selenium?

We can close the pop up window with Selenium. The getWindowHandles and getWindowHandle methods are used for the pop up window. To store all the window handles opened in a Set data structure, the getWindowHandles method is used. To store the window handle of the pop up in focus, the getWindowHandle method is used.

Can we handle window popup in Selenium?

Yes, it is possible to handle Windows based pop-ups in Selenium webdriver. Sometimes on clicking a link or a button, another window gets opened. It can be a pop up with information or an advertisement. The methods getWindowHandles and getWindowHandle are used to handle child windows.


2 Answers

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

solved the issue for me

like image 117
Maalamaal Avatar answered Sep 20 '22 14:09

Maalamaal


IAlert alert = driver.SwitchTo().Alert();  alert.Accept(); //for two buttons, choose the affirmative one // or alert.Dismiss(); //to cancel the affirmative decision, i.e., pop up will be dismissed and no action will take place 
like image 44
Brian121212 Avatar answered Sep 19 '22 14:09

Brian121212