在调试我的一个硒脚本时,我断断续续地org.openqa.selenium.remote.UnreachableBrowserException
。为了进一步调试它,我编写了一个代码来捕获WebDriverException
(尝试/捕获)并在脚本遇到WebDriverException
时获取屏幕截图。
在这里,我很惊讶地发现,即使获得UnreachableBrowserException
,屏幕截图实际上仍在被拍摄。
不是UnreachableBrowserException
表示该会话不可访问,因此应使其无效。因此,对该无效会话的webdriver对象执行的任何操作都应抛出WebDriverException
。
如果我错了,请纠正我,并且 请解释上面的奇迹(即使在获取UnreachableBrowserException
之后,屏幕截图也能正常工作)
出于分析目的,我在此处附加stacktrace
请参阅此处,尝试获取浏览器的当前URL时,我得到UnreachableBrowserException
,但是屏幕截图捕获有效,并且我可以看到屏幕截图已保存在该位置
org.openqa.selenium.remote.UnreachableBrowserException: Error communicating with the remote browser. It may have died.
Build info: version: '3.12.0', revision: '7c6e0b3', time: '2018-05-08T14:04:26.12Z'
System info: host: 'mymachine', ip: 'fe80:0:0:0:1c57:d76e:b55f:37ce%en0', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.12.6', java.version: '1.8.0_121'
Driver info: driver.version: RemoteWebDriver
Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.36.540469 (1881fd7f864150..., userDataDir: /var/folders/ym/dtyc5vz9785...}, cssSelectorsEnabled: true, databaseEnabled: false, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: MAC, platformName: MAC, rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, unexpectedAlertBehaviour: , unhandledPromptBehavior: , version: 67.0.3396.99, webStorageEnabled: true, webdriver.remote.sessionid: f4748ff254f6fe0418a37e3ab40...}
Session ID: f4748ff254f6fe0418a37e3ab40c3604
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:564)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:600)
at org.openqa.selenium.remote.RemoteWebDriver.getCurrentUrl(RemoteWebDriver.java:281)
at getCurrentURL(MyDriverBot.java:1616)
at Capture.main(Capture.java:31)
Caused by: java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:210)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at okio.Okio$2.read(Okio.java:139)
at okio.AsyncTimeout$2.read(AsyncTimeout.java:237)
at okio.RealBufferedSource.indexOf(RealBufferedSource.java:345)
at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:217)
at okhttp3.internal.http1.Http1Codec.readHeaderLine(Http1Codec.java:212)
at okhttp3.internal.http1.Http1Codec.readResponseHeaders(Http1Codec.java:189)
at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:88)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:125)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
at okhttp3.RealCall.execute(RealCall.java:77)
at org.openqa.selenium.remote.internal.OkHttpClient.execute(OkHttpClient.java:105)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:155)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:543)
... 10 more
19:25:43.232 [Capture][captureScreenShotOnDashboardException] - Start Capturing screenshot (in case of exception is found
19:25:43.233 [Capture][takeDashboardScreenshot] - Screenshot will be kept at location : /Users/myuserName/result/myException.png
19:25:44.084 [Capture][captureScreenShotOnDashboardException] - End Capturing dashboard (in case of dashboard exception is found
更新 这确实是一个真正的问题,因此在硒github上提出了问题
https://github.com/SeleniumHQ/selenium/issues/6170
更新2 现在已在硒中修复-3.14版本
答案 0 :(得分:0)
确实,这是'OKHTTP'的问题,我尝试通过以下2件事来确认这一点
1)在硒v3.13中添加了System.setProperty("webdriver.http.factory", "apache");
来强制硒使用'apache'而不是'okhttp'。它有效,我没有遇到随机UnreachableBrowserException
异常(测试了2/3次),但是执行(在识别元素方面)太慢了(我不知道为什么)
2)我将硒版本降级为使用3.8.1(从3.13开始)并重启了我的机器。再次建立我的gradle项目并执行代码2/3次。同样,我没有遇到UnreachableBrowserException
例外。
上面的结论是看来'okhttp'似乎有问题。截至目前,我已经迁移到硒3.8.1,并且一切正常。