Selenium WebDriver how to close browser popup

Viewed 82757

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?

9 Answers

You can interact with alerts and the like using the Alert API. Switching to the alert and confirm it would look like this (in Java):

Alert alert = driver.switchTo().alert();
alert.accept();

This is also explained in the Selenium FAQ.

Related