用Selenium WebDriverJsclosures警报

我一直在努力尝试closures警报浏览器打开只有一个特定的模式,我不控制,所以我需要一段代码,检测并closures警报能够继续浏览网页,而不是停止执行。

我尝试了几种处理警报的方法,但目前没有一个成功:

以下是我一直在使用的一些示例:

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:注入这段代码试图覆盖浏览器的警报,使其不显示:

  driver.executeScript('alert = function(){};'); 

基本上这三个是我一直在努力的主要解决方法,但目前似乎没有任何工作正常。

正如我理解的正确,你想closures页面上的任何警报,所以你可以尝试在你的代码中使用这个类

 UnexpectedAlertBehaviour.class 

另见这个postorg.openqa.selenium.UnhandledAlertException:打开意外的警报

这是最后的工作,以closures所有页面之间的导航整个出现的警报/确认:

  driver.executeScript('alert = function(){};'); driver.executeScript('confirm = function(){};');