In one of my scenario I am filling the form but not submitting due to my requirement. Now I am closing the browser but while closing browser it is browser an alert by saying "Are you sure .... ". How to handle this popup in firefox.

enter image description here

  • Have you tried to add an event listener to the "close" event via JS that confirms the alert if triggered? That would be my solution for this problem since the injected code stays alive even after the WebDriver is killed off. – Daniel Apr 23 '18 at 13:33
  • Could you please share the sample code...It sounds it helps – QAMember Apr 23 '18 at 13:42
  • What is the purpose of the test? What exactly are you testing? How do you close the browser? If you use driver.quit() I am not sure you will be able to interact with the browser in any way since quit() performs some internal "destruction" work except of just closing window. – Alexey R. Apr 23 '18 at 17:12

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 targeted driver to ignore the dialog :
// 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);
  • Inject some JavaScript into the page to prevent the dialog :
string JS_DISABLE_UNLOAD_DIALOG = "Object.defineProperty(BeforeUnloadEvent.prototype, 'returnValue', { get:function(){}, set:function(){} });"
  • Hey @Florent, Thanks for your soultions. 2nd solution worked for me like a charm!...Thank you soo much – QAMember Apr 25 '18 at 9:24
  • All of them are working with the latest version of Firefox. Though it should be driver.switchTo().alert().accept(); instead of driver.switchTo().alert().dismiss(); for the first one. – Florent B. Apr 25 '18 at 9:27
  • May be true.In my scenario 2nd option is working fine and 3rd option is not working for me. 1st option is not suitable to my scenario – QAMember Apr 25 '18 at 9:31
  • Which version of Firefox are you using? I'd like to find-out why the 3rd one is not working. Do you have a frame in the page? – Florent B. Apr 25 '18 at 9:33
  • Firefox version - 59.0.2 (64-bit) – QAMember Apr 25 '18 at 9:35

I have one test that results in a modal dialog popping up with a yes/no question.

I always was to press Yes and the code I use is....

  • this popup is appearing after webDriver.quit() call. Hence I can't use above code as it trough invalid session exception – QAMember Apr 23 '18 at 12:03
  • How about unchecking the 'warn me' message so it no longer appears? – Chris Adams Apr 23 '18 at 12:13

After some research I found other people having similar issues. So I would suggest the same to you:

IJavaScriptExecutor js = driver as IJavaScriptExecutor;
js.ExecuteScript("window.onbeforeunload = null;");

Use this code before driver.quit(); to avoid closure popups.

  • This is not working..Do we have any alternative solution for this. – QAMember Apr 23 '18 at 13:20
  • I suggest looking at the answers in the link I posted. There are other solutions that might solve your issue. – Runescape Apr 23 '18 at 13:25

You can use:

WebDriver.switchTo().alert().accept(); --> for accepting the alert and

WebDriver.switchTo().alert().dismiss(); --> for dismissing the alert.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.