我一直在努力尝试关闭浏览器仅以我无法控制的特定模式打开的警报,因此我需要一段代码来检测并关闭警报,以便能够继续浏览网络而不是停止执行。
我尝试了几种处理警报的方法,但目前都没有成功:
以下是我一直在使用的一些示例:
1:
driver.wait(driver.switchTo().activeElement().then(
function() {
console.log("alert detected");
driver.switchTo().alert().accept();
},
function() {
console.log("no alert detected");
}
), 1000);
2:
driver.wait(driver.switchTo().alert().accept()
.then(null, function(err)
{
console.log("ERROR ALERT");
console.log(err);
console.log("ERROR NAME");
console.log(err.name);
}), 1000);
3:注入(inject)这段代码尝试覆盖浏览器的警告,使其不出现:
driver.executeScript('alert = function(){};');
基本上,这三个是我一直在努力解决的主要解决方法,但目前似乎没有一个能正常工作。
最佳答案
据我正确理解,您想关闭页面上的任何警报应用程序,因此您可以尝试在代码中使用此类
UnexpectedAlertBehaviour.class
另请参阅这篇文章 org.openqa.selenium.UnhandledAlertException: unexpected alert open
关于javascript - 使用 Selenium WebDriverJs 关闭警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44153527/