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
    Commented Apr 23, 2018 at 13:33
  • Could you please share the sample code...It sounds it helps
    – QAMember
    Commented Apr 23, 2018 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.
    Commented Apr 23, 2018 at 17:12

4 Answers 4


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
    Commented Apr 25, 2018 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.
    Commented Apr 25, 2018 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
    Commented Apr 25, 2018 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.
    Commented Apr 25, 2018 at 9:33
  • Firefox version - 59.0.2 (64-bit)
    – QAMember
    Commented Apr 25, 2018 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
    Commented Apr 23, 2018 at 12:03
  • How about unchecking the 'warn me' message so it no longer appears? Commented Apr 23, 2018 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
    Commented Apr 23, 2018 at 13:20
  • I suggest looking at the answers in the link I posted. There are other solutions that might solve your issue.
    – Runescape
    Commented Apr 23, 2018 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 and acknowledge you have read our privacy policy.

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