Spread the love

How To Resolve UnhandledAlertException While working with Alert Box in WebDriver in Selenium

Spread the love

Many a times, while testing web application, we face such situation where we need to handle javascript alert from our webdriver code in selenium.

And it is also very common that we get the below written UnhandledAlertException   while running our code.

The code may look like-

:org.openqa.selenium.UnhandledAlertException: Modal dialog present: The account has been modified, discard changes?
Build info: version: ‘2.39.0’, revision: ‘ff23eac’, time: ‘2013-12-16 16:12:12’
System info: host: ‘HOST-ABCD’, ip: ‘, os.name: ‘Windows Server 2008 R2’, os.arch: ‘amd64’, os.version: ‘6.1’, java.version: ‘1.7.0_51’
Session ID: ad3746bf-4c03-4de9-9bb8-17831538c562
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities [{platform=WINDOWS, javascriptEnabled=true, elementScrollBehavior=0, ignoreZoomSetting=false, enablePersistentHover=true, ie.ensureCleanSession=false, browserName=internet explorer, enableElementCacheCleanup=true, unexpectedAlertBehaviour=dismiss, version=8, ie.usePerProcessProxy=false, cssSelectorsEnabled=true, ignoreProtectedModeSettings=false, requireWindowFocus=false, handlesAlerts=true, initialBrowserUrl=http://localhost:9493/, ie.forceCreateProcessApi=false, nativeEvents=true, browserAttachTimeout=0, ie.browserCommandLineSwitches=, takesScreenshot=true}]

The code we use to combat the alert box is given below:


Let us understand why this exception comes then:
It is seen many a time the application is slow in nature and selenium is trying to execute the code before the alert pop up. So the next statement will have this exception. Many coder try to put a Thread.Sleep(5) before the above code. But the success rate to avoid this exception is upto the speed of application.

So how to resolve this issue?

Well instead of Thread.Sleep(5) use a more correct method..

WebDriverWait wait =new WebDriverWait(driver, 15);

So this code will wait for the alert to come and then it will click on ok what accept button.

READ  Commonly Used Clipboard Objects in Testing

Problem might solve, but is this the correct solution??

Probably, No!!!.
This condition does not hold good for the case where the pop up do not appear. It will go to infinite loop. Let us make it little safe:

public boolean isAlertPresent(){
return true;
catch(Exception e){
return false;



There is one more beautiful way to solve this issue:

READ  Learn How to Structure of Automation Test Case Free And Fast in 2020

for(String s: webDriver.getWindowHandles()){
//see if alert exists here.

Spread the love
Animesh Chatterjeehttps://techtravelhub.com/
I am the founder and owner of the blog - TechTravelHub.com, always love to share knowledge on test automation,tools,techniques and tips.I am a passionate coder of Java and VBScript.I also publish articles on Travel ideas and great honeymoon destinations.Apart from these, I am a gear-head,love to drive across India. I have shared lots of articles here on How to travel several parts of India.Customization of cars aka car modification is my another hobby.Get in touch with me on ani01104@gamil.com

Related Articles

How to Resolve NoSuchSessionException in selenium free?

How to Resolve NoSuchSessionException in selenium? NoSuchSessionException in Selenium is a tricky...



Please enter your comment!
Please enter your name here

Recent Posts

Super Keyword in Java Simplified

Super Keyword Super Keyword in Java Simplified: Java defines a special...

Learn Object Repository in UFT in An Efficient and Fast Manner

Object Repository in UFT The post will cover the following...

Learn What Primary Tools For Test Automation

Primary Tools For Test Automation According to the Gartner report...


Spread the love