Question

how to dismiss a alert box by clicking "OK" button? I saw on WebDriver Google group that Simon is adding this feature. I am not sure whether this is supported or not.

Was it helpful?

Solution

driver.findElement(By.id("updateButton")).click();

//pop up with id "updateButton" opens

Alert alert = driver.switchTo().alert();
//update is executed
alert.accept();

OTHER TIPS

Take a look at issue 27 in the Google Code Issues List. Use the JavascriptExecutor to override the browser's alert:

((JavascriptExecutor)driver).executeScript("window.alert = function(msg){};");

It's a similar solution to handle confirmation dialogs.

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

This is a pretty popular request so hopefully it'll get implemented soon. The downside to these approaches is that it's not easy to validate the text in the message. You may want to step down to Selenium to do that.

C# code:

IAlert alert = driver.SwitchTo().Alert();
alert.Accept(); 
System.Threading.Thread.Sleep(milliseconds);

Python code:

driver.switch_to_alert().accept()

See:

site-packages/selenium/webdriver/remote/webdriver.py

site-packages/selenium/webdriver/common/alert.py

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top