Explore Courses Blog Tutorials Interview Questions
0 votes
in DevOps and Agile by (19.7k points)

I am using a Chrome Driver and trying to test a webpage.

Normally it runs fine but sometimes I get exceptions--

org.openqa.selenium.UnhandledAlertException: unexpected alert open

 (Session info: chrome=38.0.2125.111)

 (Driver info: chromedriver=2.9.248315,platform=Windows NT 6.1 x86) (WARNING: The server did not  provide any stacktrace information)

 Command duration or timeout: 16 milliseconds: null

 Build info: version: '2.42.2', revision: '6a6995d', time: '2014-06-03 17:42:30'

 System info: host: 'Casper-PC', ip: '', 'Windows 7', os.arch: 'x86', os.version:  '6.1', java.version: '1.8.0_25'

 Driver info:

Then i tried to handle the alert --

Alert alt = driver.switchTo().alert();


But this time i recived-- org.openqa.selenium.NoAlertPresentException

I am attaching the screen shots of the alert- First Alert and by using esc or enter i gets the second alertSecond Alert

I am not able to figure out what to do now. The problem is i am not receiving this exception always. And when it occurs then the test fails.

1 Answer

0 votes
by (62.9k points)

This is happening because of the default behaviour of the driver when it encounters an alert. The default behaviour was set to "ACCEPT", thus the alert was closed automatically, and the switchTo().alert() couldn't find it explicitly.

The solution is to modify the default behaviour of the driver to ("IGNORE"), so that it doesn't close the alert but just ignores it:

DesiredCapabilities dc = new DesiredCapabilities();

dc.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.IGNORE);

d = new FirefoxDriver(dc);

Then you can handle it easily and use the try-catch exception handling code to catch an error if any:

try {


} catch (UnhandledAlertException f) {

    try {

        Alert alert = driver.switchTo().alert();

        String alertText = alert.getText();

        System.out.println("Alert data: " + alertText);


    } catch (NoAlertPresentException e) {




If you are interested to learn Selenium on a much deeper level and want to become a professional in the testing domain, check out Intellipaat’s automation testing course

org.openqa.selenium.UnhandledAlertException: unexpected alert open
by (100 points)
For me, In EdgeBrowser, It's showing an Exception even after followed the above mentioned steps. Kindly lemme know the solution

Browse Categories