UnhandledAlertException:意外警报打开:{Alert text:成功保存。}

时间:2018-12-15 15:35:49

标签: selenium selenium-webdriver webdriver selenium-chromedriver

在处理使用Selenium中的Chrome驱动程序弹出的警报时,我面临以下错误。

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

场景:

当两个警报立即出现时,就会发生这种情况。

出现第一个警报弹出窗口,我正在使用:

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

立即出现下一个警报,我使用了wait,然后实施了另一个警报

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

但这失败了。

执行将在第一个警报时停止。

完全错误:

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

1 个答案:

答案 0 :(得分:0)

如果忽略警报不会影响您的情况,您可以简单地使用ovverride警报方法来解决此问题,方法是在导致错误的警报之前执行JavaScript代码:

window.alert = function() {};