如何在确认弹出窗口中单击“离开页面”按钮

How to click on 'Leave Page' button in the confirmation popup?

本文关键字:离开 离开页面 按钮 单击 确认 窗口      更新时间:2023-09-26

当我尝试关闭浏览器窗口时,我看到一个弹出窗口,上面写着"此页面要求您确认要离开 - 您输入的数据可能无法保存",并带有两个按钮"离开页面"和"留在页面上"。我想点击"离开页面"。请让我知道我该怎么做。我得到了答案,但它正在关闭弹出窗口。

(( JavascriptExecutor ) webDriver).executeScript( "window.close()" ); 

请让我知道如何解决这个问题。

尝试以下操作:-

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

它将像这样处理浏览器操作

如果您正在寻找一种方法来一直调用处理警报(如果它存在并且不执行任何其他操作):

void handleUnexpectedAlert() {
    try {
        Alert alert = driver.switchTo().alert();
        alert.accept();
    } catch (NoAlertPresentException ignore) {
        // This is the way to know it wasn't there
    }
}