2

I am expecting a pop-up upon closing the browser. For example, when you are shopping and you have items in cart and in the middle of the checkout process, you wanted to close the tab/browser, at that time the site should give you a pop-up asking "Are you sure you want to leave?" or something similar!

I'm using Selenium with python.

When I do, driver.close() it just closes the browser. It does not give any pop up.

Is there any other method that I can use?

  • Instead of closing the browser all together, try using Key commands which simulate browser closing - Ctrl + W shortcut is one way. Or can you give me an example, where you observe this. – demouser123 Aug 27 '15 at 4:02
  • It is depends on site I think , Have you seen it for any site and in any browser? – Helping Hands Aug 27 '15 at 7:24
2

Make sure you have a JavaScript popup handler for your test before closing the browser.

browser = webdriver.Firefox()

browser.get("http://abc.com")

try:

alert = browser.switch_to_alert()
alert.accept()  # If you don't want to accept it it's alert.dismiss()
browser.close()

else:

print("No popup")

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.