0 votes
1 view
in Devops and Agile by (19.8k points)

I am writing tests for a web App using Selenium web driver 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

1 Answer

0 votes
by (63.4k points)

There're at least 3 ways to handle this case.

Refresh the page and then dismiss the dialog if the driver supports it :




Setup the browser oriented driver to avoid the prompt :

// Chrome

ChromeOptions options = new ChromeOptions();


driver = new ChromeDriver(options);


// Firefox

FirefoxOptions options = new FirefoxOptions();

options.addPreference("dom.disable_beforeunload", true)

WebDriver driver = new FirefoxDriver(options)

Add some JavaScript code into the page to escape the prompt:

string JS_DISABLE_UNLOAD_DIALOG = "Object.defineProperty(BeforeUnloadEvent.prototype, 'returnValue', { get:function(){}, set:function(){} });"



Hope this helps!
Welcome to Intellipaat Community. Get your technical queries answered by top developers !