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?
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.
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.
( ( JavascriptExecutor ) _driver ) .executeScript( "window.onbeforeunload = function(e){};" );
solved the issue for me
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With