Reputation: 802
I've been struggling trying to close an alert the browser opens only with a specific patterns I don't control, so I need a piece of code that detects and closes the alert to be able to continue navigating the web and not stopping the execution.
I tried several ways of dealing with the alert, but none of them has been successful for the moment:
Here are some of the examples I've been working with:
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 this piece of code trying to overwrite the alert of the browser to make it to not appear:
driver.executeScript('alert = function(){};');
Basically these three are the main workarounds I've been struggling with, but for the moment none seems to work properly.
Upvotes: 2
Views: 1136
Reputation: 802
That's what finally worked to close all the emerging alert/confirms throughout the navigation between all pages:
driver.executeScript('alert = function(){};');
driver.executeScript('confirm = function(){};');
Upvotes: 1
Reputation: 125
As I understood correctly you would like to close any of alerts appers on page, so you can try to use this class in your code
UnexpectedAlertBehaviour.class
see also this post org.openqa.selenium.UnhandledAlertException: unexpected alert open
Upvotes: 1