Selenium Eror: org.openqa.selenium.UnhandledAlertException: unexpected alert open

+1 vote

I'm using ChromeDriver to test a webpage. Usually, it works without a problem but sometimes I get this exception:

 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: '10.0.0.4', os.name: 'Windows 7', os.arch: 'x86', os.version:  '6.1', java.version: '1.8.0_25'
 Driver info: org.openqa.selenium.chrome.ChromeDriver

So, to get rid of this, I handled this alert:

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

But this does not work and now i get no element exception: org.openqa.selenium.NoAlertPresentException

Below is the screenshot:-

Can someone suggest the solution? Because I do not receive this exception always. And when it occurs then the test fails.

May 29, 2018 in Selenium by eLiJha
• 720 points
4,001 views

1 answer to this question.

+1 vote

Even I had a similar problem. This happens because of driver's default behaviour when it encounters an alert. The default behaviour is "ACCEPT". That's why the alert gets closed automatically, and switchTo().alert() cannot find it.

By modifying the default behaviour of the driver to ("IGNORE"), the alert will not close. This is how you do it:

DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.IGNORE);
d = new FirefoxDriver(dc);

You can then handle it with this:

try {
    click(myButton);
} catch (UnhandledAlertException f) {
    try {
        Alert alert = driver.switchTo().alert();
        String alertText = alert.getText();
        System.out.println("Alert data: " + alertText);
        alert.accept();
    } catch (NoAlertPresentException e) {
        e.printStackTrace();
    }
}
answered May 29, 2018 by sniffy_god
• 780 points
it really worked very well. Thank you so much .
Glad it worked for you. Please feel free to ask if you have any other queries.
this solution is deprecated now
@Dinesh, can you suggest the better solution to this problem?

Related Questions In Selenium

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Error: org.openqa.selenium.ElementNotInteractableException

Try to use javascriptExecutor to click the ...READ MORE

answered Jul 30, 2018 in Selenium by Samarpit
• 5,130 points
1,581 views
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,090 points
759 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
1,000 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
713 views
0 votes
2 answers

When executing my Jenkins tests with Selenium WebDriver, I do not see a GUI.

Hi eLiJha, I also faced the same issue ...READ MORE

answered Jul 15 in Selenium by nayan
• 160 points
1,223 views
+2 votes
1 answer

I want the console.log output from Chrome. I'm working with selenium on Python

So this is how you do it ...READ MORE

answered May 3, 2018 in Selenium by sniffy_god
• 780 points
11,244 views