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

0 votes

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
• 700 points
2,258 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

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
• 760 points

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
524 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,030 points
75 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
448 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
56 views
0 votes
1 answer
+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
• 760 points
5,394 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.