When I try to close the browser window, I was displayed with a popup saying "This page is asking you to confirm that you want to leave-data you have entered may not be saved." with two buttons "Leave page" and "Stay on Page". I want to click on 'Leave Page'.Please let me know how can I do that. I got an answer, but it is closing the popup.
(( JavascriptExecutor ) webDriver).executeScript( "window.close()" );
Please let me know how to resolve this.
When I try to close the browser window, I was displayed with a popup saying "This page is asking you to confirm that you want to leave-data you have entered may not be saved." with two buttons "Leave page" and "Stay on Page". I want to click on 'Leave Page'.Please let me know how can I do that. I got an answer, but it is closing the popup.
(( JavascriptExecutor ) webDriver).executeScript( "window.close()" );
Please let me know how to resolve this.
Share Improve this question asked Feb 26, 2016 at 9:42 RahulRahul 7593 gold badges22 silver badges47 bronze badges 2- 1 have you try with driver.switchTo().alert().accept(); ,.... it works sometimes – Shubham Jain Commented Feb 26, 2016 at 9:44
- working for me. Thanks!! – Rahul Commented Feb 26, 2016 at 9:51
2 Answers
Reset to default 6Try Below:-
driver.switchTo().alert().accept();
It will handle browser operation like this
If you are looking for a method to call all the time that deals with the alert if it is there and does nothing otherwise:
void handleUnexpectedAlert() {
try {
Alert alert = driver.switchTo().alert();
alert.accept();
} catch (NoAlertPresentException ignore) {
// This is the way to know it wasn't there
}
}