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
• 770 points
15,289 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
2 answers

Selenium Webdriver Error: org.openqa.selenium.WebDriverException: unknown error: call function result missing 'value'

Facing Error Exception in thread "main" org.openqa.selenium .WebDriverException: unknown ...READ MORE

answered Mar 5, 2020 in Selenium by anonymous
10,220 views
0 votes
4 answers
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,910 points
7,798 views
0 votes
2 answers

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,500 points
13,676 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,144 views
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,910 points
7,221 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,875 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, 2019 in Selenium by nayan
• 160 points
13,376 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
41,837 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP