Traveller87
Traveller87

Reputation: 151

How to handle the javascript alert using webdriver with IEDriver

With the selenium IEwebdriver, the webdriver Alert class methods are not working ... This issue seems specific to the IE browser, while in chrome,ff etc this works well.

Any workarounds that we can use to handle the js alerts using IEWebdriver ..?

I tried with the javascriptexecutor method too as below

((JavascriptExecutor)driver).executeScript("window.alert = function(msg){return true;};");  
((JavascriptExecutor)driver).executeScript("window.prompt = function(msg) { return true; }");        
((JavascriptExecutor)driver).executeScript("window.confirm = function(msg) { return true; }");

But no luck :(

Any help is highly appreciated

Upvotes: 0

Views: 7787

Answers (1)

Abhishek Singh
Abhishek Singh

Reputation: 9745

In java we do it like this:

WebDriverWait wait = new WebDriverWait(driver, TIMEOUT);
wait.until(ExpectedConditions.alertIsPresent());

Above code will implicitly wait for an alert. It will throw a TimeoutException if alert is not present.

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

Above code switches to alert popup and clicks OK. If alert is not present then it will throw NoAlertPresentException.

Hope it helps.

Upvotes: 2

Related Questions