最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to click on 'Leave Page' button in the confirmation popup? - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 6

Try 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
    }
}
发布评论

评论列表(0)

  1. 暂无评论