Take the 2-minute tour ×

I need to click on a cancel button for a Java based application. I am using IE Driver, Eclipse IDE and my application only supports IE.[i am scripting in Java]

Here is the situation,

  1. Login to the application
  2. There is a account session popup[confirmation box][js] [The alert has the focus, user cannot focus the application]
  3. Click on the cancel button

Now, i have logged in successfully but i am unable to handle the JS Alert window.So i am unable to write further scripts.

Kindly help me out !!!

share|improve this question

2 Answers 2

If you're using Java, and you're using the Selenium WebDriver API, something like the following code should work:


Alert handling has not been implemented for every driver, but it should work for IE.

Remember you can always find the Javadocs for the WebDriver API at this link.

share|improve this answer
Why doesn't WebDriver support isAlertPresent API? SwitchTo with a catch block feels quirky. OTOH, if I miss checking for an alert the next API throws UnhandeledAlertExe automatically dismissing the alert. Jim, why does webdriver dismisses the alert when it throws Unhandeled Alert Exe? Assume, I don't want to write preemptively check for alerts, this provides me no way to accept alerts after its occurred. Asking you, since you are a core contributor to IE driver and have the insight to this decision to dismiss alerts on exception. Thanks –  Ashwin Prabhu Jan 11 '13 at 9:46
You should look into the unhandledAlertBehaviour capability. It provides exactly the control you're looking for as to whether the UnhandledAlertException should dismiss the alert or not. As for why there is no isAlertPresent() API, you'd have to ask the API architects, of which I'm not one. –  JimEvans Jan 11 '13 at 14:03
I did notice that. But seems like its IE only for now. Doesn't seem to be adopted by FF and chrome yet. But nice addition. WebDriver should have allowed the test author to make the accept/dismiss decision after catching the unhandeled alert exe from day 1. –  Ashwin Prabhu Jan 11 '13 at 14:04
There's another school of thought that says that the author of a piece of automation code should know what to expect from the actions taken by that automation. Anything else is an exceptional case, which is the very definition of something that should throw an exception. If you want a library that handles every unexpected thing that could possibly happen when automating a web page, that's up to you to build; it's outside the scope of WebDriver. Having said that, this is entirely the wrong place to be having this discussion. One of the mailing lists would be a much better venue. –  JimEvans Jan 11 '13 at 14:15

To Handle Alert in IE, you need to set the capabalities for IE first:

DesiredCapabilities capabilities = new DesiredCapabilities();          
capabilities.setJavascriptEnabled(true);                                           capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);                                   
capabilities.setCapability("ignoreProtectedModeSettings", true); //added this to ignore protecion mode setting so as to launch IE

driver = new InternetExplorerDriver(capabilities);
driver.switchTo().alert().dismiss();   //or  
driver.switchTo().alert().Accept();    // accordingly
share|improve this answer
These capabilities have nothing whatsoever to do with alert handling. In fact, it is generally a best practice to not set the ignoreProtectedModeSettings capability. This blog post explains why. –  JimEvans May 13 '13 at 12:56

Your Answer


