3

I'm facing below error while handling an alert pop up with Chrome driver in Selenium.

Exception in thread "main" org.openqa.selenium.UnhandledAlertException: unexpected alert open: {Alert text : Save successfully.}

Scenario:

This is happening when two alerts appears immediately.

First alert pop up appears and I'm handling it with :

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

Immediately the next alert appears and I have used wait and then implemented another alert

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

But this fails.

Execution is stopping at 1st alert.

Complete Error:

Exception in thread "main" org.openqa.selenium.UnhandledAlertException: unexpected alert open: {Alert text : Save successfully.}
(Session info: chrome=71.0.3578.98)
(Driver info: chromedriver=2.44.609538 (b655c5a60b0b544917107a59d4153d4bf78e1b90),platform=Windows NT 10.0.10586 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds: null
Build info: version: '3.12.0', revision: '7c6e0b3', time: '2018-05-08T15:15:03.216Z'
System info: host: 'KADALI7', ip: '15.75.149.101', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '10.0.1'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.44.609538 (b655c5a60b0b54..., userDataDir: C:\Users\kadali\AppData\Loc...}, cssSelectorsEnabled: true, databaseEnabled: false, goog:chromeOptions: {debuggerAddress: localhost:2130}, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, unexpectedAlertBehaviour: ignore, unhandledPromptBehavior: ignore, version: 71.0.3578.98, webStorageEnabled: true}
Session ID: 2175d3b0d47212f94e61f6e71c8ca535
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.base/java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:172)
at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:4
Denis Zavedeev
  • 7,627
  • 4
  • 32
  • 53
Pdk
  • 53
  • 1
  • 8
  • Update the question with your code trials how you _have used wait_. – undetected Selenium Dec 15 '18 at 19:28
  • driver.switchTo().alert().accept(); // fail confirmation try { Thread.sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } Alert alert112 = driver.switchTo().alert(); alert112.accept(); – Pdk Dec 16 '18 at 10:41
  • One more way of doing this could be - first check if the alert is present or not and in case yes, initialize the alert and accept it. – Neha Dec 17 '18 at 11:16

1 Answers1

2

If ignoring the alert will not affect your scenario, you can simply ovverride alert method to overcome the issue by executing javascript code before the alert which is causing the error exactly:

window.alert = function() {};
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Walid Ahmed
  • 243
  • 1
  • 12
  • Could you please give more details on how to implement "window.alert = function() {};". I'm new to this – Pdk Dec 16 '18 at 11:02
  • You can add the following to your code before the second alert exactly: JavascriptExecutor jse = (JavascriptExecutor) driver; jse.executeScript("window.alert = function() {};"); – Walid Ahmed Dec 16 '18 at 16:25
  • Could you do the same for confirm: You can add the following to your code before the second alert exactly: JavascriptExecutor jse = (JavascriptExecutor) driver; jse.executeScript("window.confirm = function() {};"); – Walid Ahmed Dec 16 '18 at 19:01
  • Thanks a lot, it worked. Inserted both jse.executeScript("window.alert = function() {};"); and jse.executeScript("window.confirm = function() {};"); before the alert. – Pdk Dec 17 '18 at 16:20