在调试我的一个selenium脚本时,我得到了间歇性的org.openqa.selenium.remote.UnreachableBrowserException
。为了进一步调试它,我编写了一个代码来捕获WebDriverException
(try/catch(&只要脚本符合WebDriverException
,就截屏。
在这里,我很惊讶地看到屏幕截图实际上是在获得UnreachableBrowserException
之后拍摄的
不UnreachableBrowserException
意味着会话不可访问,因此它应该无效。因此,对该无效会话的webdriver对象执行的任何操作都应该抛出WebDriverException
。
如果我错了就纠正我&请解释上述奇迹(即使在获得UnreachableBrowserException
后,截屏也能工作(
出于分析目的,我在此处附加堆栈跟踪
看到这里,当我试图获取浏览器的当前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现在在selenium-3.14版本中已修复此问题
事实上,这是"OKHTTP"的问题,我尝试了以下两件事来确认这个
1( 在selenium v3.13中添加了System.setProperty("webdriver.http.factory", "apache");
以强制selenium使用"apache"而不是"okhttp"。它起作用了,我没有得到超过随机UnreachableBrowserException
的异常(测试了2/3次(,但执行(在识别元素方面(太慢了(我不知道为什么(
2( 我将selenium版本降级为使用3.8.1(从3.13(&重新启动我的机器。再次构建我的gradle项目&执行代码2/3次。再一次,我没有得到UnreachableBrowserException
以上的例外。
以上的结论表明,"okhttp"似乎存在一些问题。到目前为止,我已经迁移到selenium 3.8.1&一切正常