首页 > 解决方案 > SessionNotCreatedException:试图通过 Java 使用 Selenium GeckoDriver 和 Firefox 运行命令而不建立连接错误

问题描述

我的浏览器在我的 selenium java 脚本中的随机位置崩溃。有时它似乎也没有将文本放入输入框中。发生这种情况时,有时我会看到一个壁虎驱动程序潜伏在任务管理器中,但并非每次都如此。

我已经捕获了壁虎跟踪日志。无法获取浏览器的转储,就像我尝试为其提供配置文件目录时一样

壁虎驱动程序的参数:[“-profile”,“C:/Temp/FFProfiles/tmp”]

,它确实在其中创建了新的配置文件,但随后无法连接到浏览器(等待一分钟然后引发错误)

环境:

Java-1.8、Win-7、tomcat-8.0.30

试过firefox驱动v-3.11.0.jar到3.141.59.jar

chrome 驱动程序 v-3.11.0.jar 到 3.141.59.jar,

selenium-server-standalone-3.11.0.jar 到 3.141.59.jar

试过无头和头..

以下是来自应用程序的堆栈跟踪:此时它在查找元素时中断(即浏览器在之前的操作中崩溃),其他时候在其他地方崩溃..

org.openqa.selenium.SessionNotCreatedException: Tried to run command without establishing a connection
Build info: version: '3.11.0', revision: 'e59cfb3', time: '2018-03-11T20:33:15.31Z'
System info: host: 'PC-17', ip: '199.248.254.10', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_65'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities {acceptInsecureCerts: true, browserName: firefox, browserVersion: 59.0.3, javascriptEnabled: true, moz:accessibilityChecks: false, moz:headless: true, moz:processID: 9584, moz:profile: C:\Users\admin\AppData\Loca..., moz:useNonSpecCompliantPointerOrigin: false, moz:webdriverClick: true, pageLoadStrategy: normal, platform: XP, platformName: XP, platformVersion: 6.1, rotatable: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}}
Session ID: a8f6763c-d54b-49e8-a2e2-fda1b8ed6462
*** Element info: {Using=xpath, value=//div[@id='addresIMB']//P}
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:545)
    at org.openqa.selenium.remote.RemoteWebDriver.findElements(RemoteWebDriver.java:346)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementsByXPath(RemoteWebDriver.java:425)
    at org.openqa.selenium.By$ByXPath.findElements(By.java:356)
    at org.openqa.selenium.remote.RemoteWebDriver.findElements(RemoteWebDriver.java:307)
    at com.selenium.test.AddressParser.getAddressErrors(AddressParser.java:96)

壁虎 TRACE 日志

1560940131819   geckodriver INFO    geckodriver 0.19.1
1560940131830   geckodriver INFO    Listening on 127.0.0.1:9814
1560940133692   mozrunner::runner   INFO    Running command: "C:/pk/Chrome_Downloads/Firefox_59.0.3_Sel_Executable/firefox.exe" "-marionette" "-width=1920" "-height=1080" "-disable-extensions" "-user-agent=Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0" "-headless" "-profile" "C:\\Users\\admin\\AppData\\Local\\Temp\\rust_mozprofile.37odkVGHuO98"
1560940134786   geckodriver::marionette TRACE     connection attempt 0/600
1560940135898   geckodriver::marionette TRACE     connection attempt 1/600
*** You are running in headless mode.
1560940137000   geckodriver::marionette TRACE     connection attempt 2/600
1560940138103   geckodriver::marionette TRACE     connection attempt 3/600
1560940139207   geckodriver::marionette TRACE     connection attempt 4/600
1560940140216   Marionette  DEBUG   Received observer notification "profile-after-change"
1560940140303   geckodriver::marionette TRACE     connection attempt 5/600
1560940140928   Marionette  DEBUG   Received observer notification "command-line-startup"
1560940140928   Marionette  INFO    Enabled via --marionette
1560940141407   geckodriver::marionette TRACE     connection attempt 6/600
1560940142509   geckodriver::marionette TRACE     connection attempt 7/600
1560940143610   geckodriver::marionette TRACE     connection attempt 8/600
1560940144794   geckodriver::marionette TRACE     connection attempt 9/600
1560940145895   geckodriver::marionette TRACE     connection attempt 10/600
1560940146996   geckodriver::marionette TRACE     connection attempt 11/600
1560940148118   geckodriver::marionette TRACE     connection attempt 12/600
1560940149223   geckodriver::marionette TRACE     connection attempt 13/600
1560940150086   Marionette  DEBUG   Received observer notification "sessionstore-windows-restored"
1560940150330   geckodriver::marionette TRACE     connection attempt 14/600
1560940151424   geckodriver::marionette TRACE     connection attempt 15/600
1560940152446   Marionette  DEBUG   Setting recommended pref toolkit.cosmeticAnimations.enabled to false
1560940152448   Marionette  DEBUG   Setting recommended pref datareporting.policy.dataSubmissionPolicyAccepted to false
1560940152450   Marionette  DEBUG   New connections are accepted
1560940152452   Marionette  INFO    Listening on port 54952
1560940152528   geckodriver::marionette DEBUG   Connected to Marionette on localhost:54952
1560940152808   Marionette  DEBUG   Accepted connection 0 from 127.0.0.1:55000
1560940152853   geckodriver::marionette TRACE   <- {"applicationType":"gecko","marionetteProtocol":3}
1560940152853   geckodriver::marionette TRACE   -> 163:[0,1,"newSession",{"acceptInsecureCerts":true,"browserName":"firefox","capabilities":{"desiredCapabilities":{"acceptInsecureCerts":true,"browserName":"firefox"}}}]
1560940152874   Marionette  TRACE   0 -> [0,1,"newSession",{"acceptInsecureCerts":true,"browserName":"firefox","capabilities":{"desiredCapabilities":{"acceptInsecureCerts":true,"browserName":"firefox"}}}]
1560940152874   Marionette  WARN    TLS certificate errors will be ignored for this session
1560940153180   Marionette  DEBUG   Register listener.js for window 4294967297
1560940153190   Marionette  TRACE   0 <- [1,1,null,{"sessionId":"a8f6763c-d54b-49e8-a2e2-fda1b8ed6462","capabilities":{"browserName":"firefox","browserVersion":"59.0. ... AppData\\Local\\Temp\\rust_mozprofile.37odkVGHuO98","moz:useNonSpecCompliantPointerOrigin":false,"moz:webdriverClick":true}}]
1560940153342   geckodriver::marionette TRACE   <- [1,1,null,{"sessionId":"a8f6763c-d54b-49e8-a2e2-fda1b8ed6462","capabilities":{"browserName":"firefox","browserVersion":"59.0.3","platformName":"windows_nt","platformVersion":"6.1","pageLoadStrategy":"normal","acceptInsecureCerts":true,"timeouts":{"implicit":0,"pageLoad":300000,"script":30000},"rotatable":false,"moz:accessibilityChecks":false,"moz:headless":true,"moz:processID":9584,"moz:profile":"C:\\Users\\admin\\AppData\\Local\\Temp\\rust_mozprofile.37odkVGHuO98","moz:useNonSpecCompliantPointerOrigin":false,"moz:webdriverClick":true}}]
1560940153345   webdriver::server   DEBUG   <- 200 OK {"value": {"sessionId":"a8f6763c-d54b-49e8-a2e2-fda1b8ed6462","capabilities":{"acceptInsecureCerts":true,"browserName":"firefox","browserVersion":"59.0.3","moz:accessibilityChecks":false,"moz:headless":true,"moz:processID":9584,"moz:profile":"C:\\Users\\admin\\AppData\\Local\\Temp\\rust_mozprofile.37odkVGHuO98","moz:useNonSpecCompliantPointerOrigin":false,"moz:webdriverClick":true,"pageLoadStrategy":"normal","platformName":"windows_nt","platformVersion":"6.1","rotatable":false,"timeouts":{"implicit":0,"pageLoad":300000,"script":30000}}}}
1560940153457   webdriver::server   DEBUG   -> POST /session/a8f6763c-d54b-49e8-a2e2-fda1b8ed6462/timeouts {"pageLoad":300000}
1560940153458   geckodriver::marionette TRACE   -> 39:[0,2,"setTimeouts",{"pageLoad":300000}]
1560940153596   Marionette  TRACE   0 -> [0,2,"setTimeouts",{"pageLoad":300000}]
1560940153598   Marionette  TRACE   0 <- [1,2,null,{}]
..............
..................
..............
..................
..............
..................
1560940604266   Marionette  TRACE   0 -> [0,191,"executeScript",{"args":[],"newSandbox":false,"script":"var source = document.documentElement.outerHTML; \nif (!source) { source = new XMLSerializer().serializeToString(document); }\nreturn source;","scriptTimeout":null,"specialPowers":false}]
1560940604292   Marionette  TRACE   0 <- [1,191,null,{"value":"<html dir=\"ltr\" class=\"noie".......... </iframe><div id=\"a-popover-modal\"></div></body></html>"}]
1560940604369   geckodriver::marionette TRACE   <- [1,191,null,{"value":"<html dir=\"ltr\" class=\"noie ......</iframe><div id=\"a-popover-modal\"></div></body></html>"}]
156094060551560940605496    Marionette  TRACE   0 -> [0,192,"quit",{"flags":["eForceQuit"]}]
1560940605498   Marionette  DEBUG   New connections will no longer be accepted
07  webdriver::server   DEBUG   -> DELETE /session/a8f6763c-d54b-49e8-a2e2-fda1b8ed6462 
JavaScript warning: https://images-na.ssl-images-abcd.com/images/G/01/AUIClients/FWCIMAssets.51c2a8513bb855c92a49e78dbeab4f9e24afeb55._V2_.js, line 385: Error: WebGL warning: Can't use WebGL in headless mode (https://bugzil.la/1375585).
JavaScript warning: https://images-na.ssl-images-abcd.com/images/G/01/AUIClients/FWCIMAssets.51c2a8513bb855c92a49e78dbeab4f9e24afeb55._V2_.js, line 385: Error: WebGL warning: Failed to create WebGL context: WebGL creation failed: 
* Can't use WebGL in headless mode (https://bugzil.la/1375585).
1560940605512   geckodriver::marionette TRACE   -> 39:[0,192,"quit",{"flags":["eForceQuit"]}]
1560940605561   webdriver::server   DEBUG   <- 200 OK {"value":"<html dir=\"ltr\" class=\"noie .........function L(a){var b=g.ue_csm_markers||{},c;for(c in b)b.hasOwnProperty(c)&&t(c,a,y,b[c])}function u(d,b,c){c=c||g;a.ue_pel&&window.EventTarget&&window.EventTarget.prototype&&window.EventTarget.prototype.addEventListener?window.EventTarget.prototype.addEventListener.call(c,d,b,!!window.ue_clf):c.addEventListener?c.addEventListener(d,b,!!window.ue_clf):c.attachEvent&&c.attachEvent(\"on\"+d,b)}\nfunction E(d,b,c){c=c||g;a.ue_pel&&window.EventTarget&&window.EventTarget.protot1ype&&window.Even560940605610   Marionette  TRACE   0 <- [1,192,null,{"cause":"shutdown"}]
...........\n\n\n1560940605764  Marionette  DEBUG   Closed connection 0
\n.......</iframe><div id=\"a-popover-modal\"></div></body></html>"}
Unable to read VR Path Registry from C:\Users\admin\AppData\Local\openvr\openvrpaths.vrpath
[Child 12684, Chrome_ChildThread] WARNING: pipe error: 109: file z:/build/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 346
Unable to read VR Path Registry from C:\Users\admin\AppData\Local\openvr\openvrpaths.vrpath
[Parent 9584, Gecko_IOThread] WARNING: pipe error: 109: file z:/build/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 346
[Child 12684, Chrome_ChildThread] WARNING: pipe error: 109: file z:/build/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 346
[Child 12312, Chrome_ChildThread] WARNING: pipe error: 109: file z:/build/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 346
[Child 12312, Chrome_ChildThread] WARNING: pipe error: 109: file z:/build/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 346
1560940605673   geckodriver::marionette TRACE   <- [1,192,null,{"cause":"shutdown"}]
1560940607022   webdriver::server   DEBUG   Deleting session
1560940607023   geckodriver::marionette DEBUG   Stopping browser process
1560940607096   webdriver::server   DEBUG   -> POST /session/a8f6763c-d54b-49e8-a2e2-fda1b8ed6462/elements {"value":"//div[@id='addressIMB']//P","using":"xpath"}
1560940608819   webdriver::server   DEBUG   <- 200 OK {"value": {}}
1560940608819   webdriver::server   DEBUG   <- 500 Internal Server Error {"value":{"error":"session not created","message":"Tried to run command without establishing a connection","stacktrace":"stack backtrace:\n   0:           0x47e934 - <no info>\n   1:           0x47f0a3 - <no info>\n   2:           0x442511 - <no info>\n   3:           0x42a686 - <no info>\n   4:           0x406f5e - <no info>\n   5:           0x40cfc9 - <no info>\n   6:           0x6bef19 - <no info>\n   7:           0x420756 - <no info>\n   8:           0x6b96e0 - <no info>\n   9:         0x770459cd - BaseThreadInitThunk"}}

标签: javaseleniumfirefoxselenium-webdrivergeckodriver

解决方案


此错误消息...

org.openqa.selenium.SessionNotCreatedException: Tried to run command without establishing a connection
Build info: version: '3.11.0', revision: 'e59cfb3', time: '2018-03-11T20:33:15.31Z'
System info: host: 'PC-17', ip: '199.248.254.10', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_65'

...暗示GeckoDriver无法启动/产生新的WebBrowsing 会话,即Firefox 浏览器会话。

您的主要问题是您使用的二进制文件版本之间的不兼容,如下所示:

  • 您的JDK 版本1.8.0_65非常古老

因此JDK v8u65Selenium Client v3.141.59之间存在明显的不匹配。


解决方案

  • 将JDK升级到最新级别JDK 8u212
  • 将Selenium升级到当前级别版本 3.141.59
  • 将GeckoDriver升级到GeckoDriver v0.24.0级别。
  • GeckoDriver 存在于指定位置。
  • GeckoDriver 对非 root 用户具有可执行权限。
  • 将Firefox版本升级到Firefox v67.0级别。

推荐阅读