当我尝试关闭浏览器窗口时,显示的是弹出窗口"此页面要求您确认要离开 - 您输入的数据可能无法保存。"有两个按钮"离开页面"和#34;留在页面"。我想点击“离开页面”。请让我知道我该怎么做。我得到了答案,但它正在关闭弹出窗口。
(( JavascriptExecutor ) webDriver).executeScript( "window.close()" );
请告诉我如何解决此问题。
答案 0 :(得分:2)
尝试下面的内容: -
driver.switchTo().alert().accept();
它将像这样处理浏览器操作
答案 1 :(得分:0)
如果您一直在寻找一种方法来处理警报(如果存在的话),否则将不执行任何操作:
void handleUnexpectedAlert() {
try {
Alert alert = driver.switchTo().alert();
alert.accept();
} catch (NoAlertPresentException ignore) {
// This is the way to know it wasn't there
}
}