Exception in thread "main" org.openqa.selenium.UnhandledAlertException: Unexpected modal dialog

612 views
Skip to first unread message

Akshatha

unread,
Aug 19, 2016, 3:08:47 PM8/19/16
to webdriver
Hello Team,

I am new to selenium webdriver.
Please help me in this regard

When there is an alert message from the web application, there is an exception in my selenium code as "Exception in thread "main" org.openqa.selenium.UnhandledAlertException: Unexpected modal dialog "

if(isAlertPresent(driver))
{
Alert confirmationAlert = driver.switchTo().alert();
String alertText = confirmationAlert.getText();
System.out.println("Alert text is " + alertText);
confirmationAlert.accept();
}

Above is the part of code I have written.

Please do the needful asap.

Thanks,
Akshatha

darrell grainger

unread,
Aug 20, 2016, 9:46:19 AM8/20/16
to webdriver
If you attempt to interact with something on the website you will get an UnhandledAlertException. This is similar to trying to click on the website when a modal dialog is present. The clicks on the website will fail because the modal dialog is in the way.

You need to figure out how to handle this. One way would we to wrap all calls in a try-catch. You would have to do something like:

boolean click_not_done = true;
while(click_not_done) {
try {
// find an element
// click the element
click_not_done = false;
} catch(UnhandledAlertException uae) {
// switch to the alert and handle it
}
}

Now this is pretty ugly and cumbersome. So although this would work, it would be difficult to maintain. Being too difficult to maintain is the number one killer to test automation. So this isn't really a good solution.

If you know when a dialog will appear, handling the alert right away would be a good idea. For example, if I had the code:

// find an element
// click the element
// alert appears because of the previous action
// switch to the alert
// handle the alert
// continue with the rest of your program

This works great but only if you know when the alert dialog will appear. The alert dialog must occur 100% of the time if you use this solution. What if you are testing a website where the dialog might appear 30% of the time? Then you can use an if statement like you have below. Your code would become:

// find an element
// click the element
// if an alert appears because of the previous action {
// switch to the alert
// handle the alert
// }
// continue with the rest of your program

You don't need to watch for the dialog on every action. You only need to do it once or twice in a test.

The really hard case is when a dialog will randomly appear (e.g. asking user if they want to participate in a survey). In these cases you can set a capability for your WebDriver instance on how to handle the unexpected alert. For example, if I create my WebDriver instance using:

WebDriver driver = new ChromeDriver();

what I can do instance is:

import org.openqa.selenium.UnexpectedAlertBehaviour;
import static org.openqa.selenium.remote.CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR;

DesiredCapabilities dc = DesiredCapabilities.chrome();
dc.setCapability(UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.DISMISS);
WebDriver driver = new ChromeDriver(dc);

By creating my WebDriver instance this way, whenever an unexpected alert appears, it will automatically cancel the dialog. You could also set the default behaviour to always ACCEPT the dialog. You do have to set the default behaviour to the same all the time.

Darrell

Mahendra Reddy

unread,
Aug 28, 2016, 12:35:05 AM8/28/16
to webd...@googlegroups.com, ananya...@gmail.com
Hi Akshatha,

It might help full for u.


If already resolved pls ignore this and write me back your solution for this issue.

--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webdriver+unsubscribe@googlegroups.com.
To post to this group, send email to webd...@googlegroups.com.
Visit this group at https://groups.google.com/group/webdriver.
For more options, visit https://groups.google.com/d/optout.



--
Thanks & Regards,

Mahendra Reddy  P,
Mob: +91- 8106611476
Skype ID: P_MahendraReddy
Reply all
Reply to author
Forward
0 new messages