无法在 selenium webdriver 上启动新会话

could not start a new session on selenium webdriver

我正在尝试使用 selenium java 代码打开 firefox 浏览器。

System.setProperty("webdriver.gecko.driver", "  System.setProperty("webdriver.gecko.driver", "C:/Program Files/Mozilla Firefox/geckodriver-v0.8.0-win32/geckodriver.exe");

WebDriver driver = new FirefoxDriver();
System.setProperty("webdriver.firefox.port","4444");

Selenium 服务器已启动并且 运行。我尝试使用命令提示符更改 selenium 服务器端口并在代码中设置端口,但它们都不起作用。

> Exception in thread "main" org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.
Build info: version: 'unknown', revision: '2aa21c1', time: '2016-08-02 14:59:43 -0700'
System info: host: 'W0045395', ip: '10.17.208.124', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_101'
Driver info: driver.version: FirefoxDriver
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:670)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:247)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:130)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:232)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:220)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:215)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:211)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:124)
    at newpackage.Gmail_Login.main(Gmail_Login.java:18)
Caused by: org.openqa.selenium.WebDriverException: org.apache.http.conn.HttpHostConnectException: Connect to localhost:6544 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect
Build info: version: 'unknown', revision: '2aa21c1', time: '2016-08-02 14:59:43 -0700'
System info: host: 'W0045395', ip: '10.17.208.124', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_101'
Driver info: driver.version: FirefoxDriver
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:91)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:649)
    ... 8 more
Caused by: org.apache.http.conn.HttpHostConnectException: Connect to localhost:6544 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:158)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353)
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:71)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)
    at org.openqa.selenium.remote.internal.ApacheHttpClient.fallBackExecute(ApacheHttpClient.java:142)
    at org.openqa.selenium.remote.internal.ApacheHttpClient.execute(ApacheHttpClient.java:88)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:142)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
    ... 9 more
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:74)
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:141)
    ... 22 more

在错误消息中,"Connect to localhost:6544" 它每次都尝试连接到不同的本地主机。谁能看出问题出在哪里?

我使用的版本是:

我的 geckodriver 可执行文件也不是 运行,它说缺少 VCRUNTIME140.dill,我应该重新安装以解决问题。但是我重新安装后,又发生了同样的事情。


更新

感谢@Saurabh 的建议,我将 selenium 降级到 2.53.1。浏览器现在正在打开,但没有任何显示。我又遇到了一个错误

    org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms. Firefox console output:
ateKey":null,"optionsURL":null,"optionsType":null,"aboutURL":null,"icons":{},"iconURL":"chrome://web2pdfextension/skin/C_AdobeLogo_48x48.png","icon64URL":"chrome://web2pdfextension/skin/C_AdobeLogo_64x64.png","defaultLocale":{"name":"Adobe Acrobat DC - Create PDF","description":"Converts a web page to PDF","creator":"Adobe Systems Incorporated","homepageURL":"http://www.adobe.com/"},"visible":true,"active":false,"userDisabled":true,"appDisabled":false,"descriptor":"C:\Program Files\Adobe\Acrobat DC\Acrobat\Browser\WCFirefoxExtn","installDate":1463491629403,"updateDate":1463491629403,"applyBackgroundUpdates":1,"bootstrap":false,"skinnable":false,"size":345393,"sourceURI":null,"releaseNotesURI":null,"softDisabled":false,"foreignInstall":true,"hasBinaryComponents":false,"strictCompatibility":false,"locales":[],"targetApplications":[{"id":"{ec8030f7-c20a-464f-9b0e-13a3a9e97384}","minVersion":"4.0","maxVersion":"46.0"}],"targetPlatforms":[],"multiprocessCompatible":false,"signedState":2,"seen":true}
1471356838157   DeferredSave.extensions.json    DEBUG   Save changes
1471356838157   addons.xpi  DEBUG   Updating database with changes to installed add-ons
1471356838157   addons.xpi-utils    DEBUG   Updating add-on states
1471356838158   addons.xpi-utils    DEBUG   Writing add-ons list
1471356838161   addons.xpi  DEBUG   Registering manifest for C:\Program Files\Mozilla Firefox\browser\features\e10srollout@mozilla.org.xpi
1471356838162   addons.xpi  DEBUG   Calling bootstrap method startup on e10srollout@mozilla.org version 1.0
1471356838162   addons.xpi  DEBUG   Registering manifest for C:\Program Files\Mozilla Firefox\browser\features\firefox@getpocket.com.xpi
1471356838162   addons.xpi  DEBUG   Calling bootstrap method startup on firefox@getpocket.com version 1.0.4
1471356838163   addons.xpi  DEBUG   Registering manifest for C:\Program Files\Mozilla Firefox\browser\features\loop@mozilla.org.xpi
1471356838164   addons.xpi  DEBUG   Calling bootstrap method startup on loop@mozilla.org version 1.4.3
1471356838181   addons.manager  DEBUG   Registering shutdown blocker for XPIProvider
1471356838181   addons.manager  DEBUG   Provider finished startup: XPIProvider
1471356838181   addons.manager  DEBUG   Starting provider: LightweightThemeManager
1471356838181   addons.manager  DEBUG   Registering shutdown blocker for LightweightThemeManager
1471356838181   addons.manager  DEBUG   Provider finished startup: LightweightThemeManager
1471356838182   addons.manager  DEBUG   Starting provider: GMPProvider
1471356838187   addons.manager  DEBUG   Registering shutdown blocker for GMPProvider
1471356838187   addons.manager  DEBUG   Provider finished startup: GMPProvider
1471356838187   addons.manager  DEBUG   Starting provider: PluginProvider
1471356838187   addons.manager  DEBUG   Registering shutdown blocker for PluginProvider
1471356838187   addons.manager  DEBUG   Provider finished startup: PluginProvider
1471356838188   addons.manager  DEBUG   Completed startup sequence
1471356839788   addons.manager  DEBUG   Starting provider: <unnamed-provider>
1471356839788   addons.manager  DEBUG   Registering shutdown blocker for <unnamed-provider>
1471356839788   addons.manager  DEBUG   Provider finished startup: <unnamed-provider>
1471356840362   DeferredSave.extensions.json    DEBUG   Write succeeded
1471356840362   addons.xpi-utils    DEBUG   XPI Database saved, setting schema version preference to 17
1471356840362   DeferredSave.extensions.json    DEBUG   Starting timer
1471356840374   addons.repository   DEBUG   No addons.json found.
1471356840374   DeferredSave.addons.json    DEBUG   Save changes
1471356840377   DeferredSave.addons.json    DEBUG   Starting timer
1471356840413   addons.manager  DEBUG   Starting provider: PreviousExperimentProvider
1471356840413   addons.manager  DEBUG   Registering shutdown blocker for PreviousExperimentProvider
1471356840413   addons.manager  DEBUG   Provider finished startup: PreviousExperimentProvider
1471356840421   DeferredSave.extensions.json    DEBUG   Starting write
1471356840440   DeferredSave.addons.json    DEBUG   Starting write
1471356840460   DeferredSave.extensions.json    DEBUG   Write succeeded
1471356840495   DeferredSave.addons.json    DEBUG   Write succeeded

    at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewProfileExtensionConnection.java:112)
    at org.openqa.selenium.firefox.FirefoxDriver.startClient(FirefoxDriver.java:271)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:119)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:218)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:211)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:129)
    at newpackage.Gmail_Login.main(Gmail_Login.java:25)
Exception in thread "main" org.openqa.selenium.WebDriverException: Failed to connect to binary FirefoxBinary(C:\Program Files\Mozilla Firefox\firefox.exe) on port 7055; process output follows: 
ateKey":null,"optionsURL":null,"optionsType":null,"aboutURL":null,"icons":{},"iconURL":"chrome://web2pdfextension/skin/C_AdobeLogo_48x48.png","icon64URL":"chrome://web2pdfextension/skin/C_AdobeLogo_64x64.png","defaultLocale":{"name":"Adobe Acrobat DC - Create PDF","description":"Converts a web page to PDF","creator":"Adobe Systems Incorporated","homepageURL":"http://www.adobe.com/"},"visible":true,"active":false,"userDisabled":true,"appDisabled":false,"descriptor":"C:\Program Files\Adobe\Acrobat DC\Acrobat\Browser\WCFirefoxExtn","installDate":1463491629403,"updateDate":1463491629403,"applyBackgroundUpdates":1,"bootstrap":false,"skinnable":false,"size":345393,"sourceURI":null,"releaseNotesURI":null,"softDisabled":false,"foreignInstall":true,"hasBinaryComponents":false,"strictCompatibility":false,"locales":[],"targetApplications":[{"id":"{ec8030f7-c20a-464f-9b0e-13a3a9e97384}","minVersion":"4.0","maxVersion":"46.0"}],"targetPlatforms":[],"multiprocessCompatible":false,"signedState":2,"seen":true}
1471356838157   DeferredSave.extensions.json    DEBUG   Save changes
1471356838157   addons.xpi  DEBUG   Updating database with changes to installed add-ons
1471356838157   addons.xpi-utils    DEBUG   Updating add-on states
1471356838158   addons.xpi-utils    DEBUG   Writing add-ons list
1471356838161   addons.xpi  DEBUG   Registering manifest for C:\Program Files\Mozilla Firefox\browser\features\e10srollout@mozilla.org.xpi
1471356838162   addons.xpi  DEBUG   Calling bootstrap method startup on e10srollout@mozilla.org version 1.0
1471356838162   addons.xpi  DEBUG   Registering manifest for C:\Program Files\Mozilla Firefox\browser\features\firefox@getpocket.com.xpi
1471356838162   addons.xpi  DEBUG   Calling bootstrap method startup on firefox@getpocket.com version 1.0.4
1471356838163   addons.xpi  DEBUG   Registering manifest for C:\Program Files\Mozilla Firefox\browser\features\loop@mozilla.org.xpi
1471356838164   addons.xpi  DEBUG   Calling bootstrap method startup on loop@mozilla.org version 1.4.3
1471356838181   addons.manager  DEBUG   Registering shutdown blocker for XPIProvider
1471356838181   addons.manager  DEBUG   Provider finished startup: XPIProvider
1471356838181   addons.manager  DEBUG   Starting provider: LightweightThemeManager
1471356838181   addons.manager  DEBUG   Registering shutdown blocker for LightweightThemeManager
1471356838181   addons.manager  DEBUG   Provider finished startup: LightweightThemeManager
1471356838182   addons.manager  DEBUG   Starting provider: GMPProvider
1471356838187   addons.manager  DEBUG   Registering shutdown blocker for GMPProvider
1471356838187   addons.manager  DEBUG   Provider finished startup: GMPProvider
1471356838187   addons.manager  DEBUG   Starting provider: PluginProvider
1471356838187   addons.manager  DEBUG   Registering shutdown blocker for PluginProvider
1471356838187   addons.manager  DEBUG   Provider finished startup: PluginProvider
1471356838188   addons.manager  DEBUG   Completed startup sequence
1471356839788   addons.manager  DEBUG   Starting provider: <unnamed-provider>
1471356839788   addons.manager  DEBUG   Registering shutdown blocker for <unnamed-provider>
1471356839788   addons.manager  DEBUG   Provider finished startup: <unnamed-provider>
1471356840362   DeferredSave.extensions.json    DEBUG   Write succeeded
1471356840362   addons.xpi-utils    DEBUG   XPI Database saved, setting schema version preference to 17
1471356840362   DeferredSave.extensions.json    DEBUG   Starting timer
1471356840374   addons.repository   DEBUG   No addons.json found.
1471356840374   DeferredSave.addons.json    DEBUG   Save changes
1471356840377   DeferredSave.addons.json    DEBUG   Starting timer
1471356840413   addons.manager  DEBUG   Starting provider: PreviousExperimentProvider
1471356840413   addons.manager  DEBUG   Registering shutdown blocker for PreviousExperimentProvider
1471356840413   addons.manager  DEBUG   Provider finished startup: PreviousExperimentProvider
1471356840421   DeferredSave.extensions.json    DEBUG   Starting write
1471356840440   DeferredSave.addons.json    DEBUG   Starting write
1471356840460   DeferredSave.extensions.json    DEBUG   Write succeeded
1471356840495   DeferredSave.addons.json    DEBUG   Write succeeded

Build info: version: '2.53.1', revision: 'a36b8b1', time: '2016-06-30 17:37:09'
System info: host: 'W0045395', ip: '10.17.208.124', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_101'
Driver info: driver.version: FirefoxDriver
    at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewProfileExtensionConnection.java:124)
    at org.openqa.selenium.firefox.FirefoxDriver.startClient(FirefoxDriver.java:271)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:119)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:218)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:211)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:129)
    at newpackage.Gmail_Login.main(Gmail_Login.java:25)
Caused by: org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms. Firefox console output:
ateKey":null,"optionsURL":null,"optionsType":null,"aboutURL":null,"icons":{},"iconURL":"chrome://web2pdfextension/skin/C_AdobeLogo_48x48.png","icon64URL":"chrome://web2pdfextension/skin/C_AdobeLogo_64x64.png","defaultLocale":{"name":"Adobe Acrobat DC - Create PDF","description":"Converts a web page to PDF","creator":"Adobe Systems Incorporated","homepageURL":"http://www.adobe.com/"},"visible":true,"active":false,"userDisabled":true,"appDisabled":false,"descriptor":"C:\Program Files\Adobe\Acrobat DC\Acrobat\Browser\WCFirefoxExtn","installDate":1463491629403,"updateDate":1463491629403,"applyBackgroundUpdates":1,"bootstrap":false,"skinnable":false,"size":345393,"sourceURI":null,"releaseNotesURI":null,"softDisabled":false,"foreignInstall":true,"hasBinaryComponents":false,"strictCompatibility":false,"locales":[],"targetApplications":[{"id":"{ec8030f7-c20a-464f-9b0e-13a3a9e97384}","minVersion":"4.0","maxVersion":"46.0"}],"targetPlatforms":[],"multiprocessCompatible":false,"signedState":2,"seen":true}
1471356838157   DeferredSave.extensions.json    DEBUG   Save changes
1471356838157   addons.xpi  DEBUG   Updating database with changes to installed add-ons
1471356838157   addons.xpi-utils    DEBUG   Updating add-on states
1471356838158   addons.xpi-utils    DEBUG   Writing add-ons list
1471356838161   addons.xpi  DEBUG   Registering manifest for C:\Program Files\Mozilla Firefox\browser\features\e10srollout@mozilla.org.xpi
1471356838162   addons.xpi  DEBUG   Calling bootstrap method startup on e10srollout@mozilla.org version 1.0
1471356838162   addons.xpi  DEBUG   Registering manifest for C:\Program Files\Mozilla Firefox\browser\features\firefox@getpocket.com.xpi
1471356838162   addons.xpi  DEBUG   Calling bootstrap method startup on firefox@getpocket.com version 1.0.4
1471356838163   addons.xpi  DEBUG   Registering manifest for C:\Program Files\Mozilla Firefox\browser\features\loop@mozilla.org.xpi
1471356838164   addons.xpi  DEBUG   Calling bootstrap method startup on loop@mozilla.org version 1.4.3
1471356838181   addons.manager  DEBUG   Registering shutdown blocker for XPIProvider
1471356838181   addons.manager  DEBUG   Provider finished startup: XPIProvider
1471356838181   addons.manager  DEBUG   Starting provider: LightweightThemeManager
1471356838181   addons.manager  DEBUG   Registering shutdown blocker for LightweightThemeManager
1471356838181   addons.manager  DEBUG   Provider finished startup: LightweightThemeManager
1471356838182   addons.manager  DEBUG   Starting provider: GMPProvider
1471356838187   addons.manager  DEBUG   Registering shutdown blocker for GMPProvider
1471356838187   addons.manager  DEBUG   Provider finished startup: GMPProvider
1471356838187   addons.manager  DEBUG   Starting provider: PluginProvider
1471356838187   addons.manager  DEBUG   Registering shutdown blocker for PluginProvider
1471356838187   addons.manager  DEBUG   Provider finished startup: PluginProvider
1471356838188   addons.manager  DEBUG   Completed startup sequence
1471356839788   addons.manager  DEBUG   Starting provider: <unnamed-provider>
1471356839788   addons.manager  DEBUG   Registering shutdown blocker for <unnamed-provider>
1471356839788   addons.manager  DEBUG   Provider finished startup: <unnamed-provider>
1471356840362   DeferredSave.extensions.json    DEBUG   Write succeeded
1471356840362   addons.xpi-utils    DEBUG   XPI Database saved, setting schema version preference to 17
1471356840362   DeferredSave.extensions.json    DEBUG   Starting timer
1471356840374   addons.repository   DEBUG   No addons.json found.
1471356840374   DeferredSave.addons.json    DEBUG   Save changes
1471356840377   DeferredSave.addons.json    DEBUG   Starting timer
1471356840413   addons.manager  DEBUG   Starting provider: PreviousExperimentProvider
1471356840413   addons.manager  DEBUG   Registering shutdown blocker for PreviousExperimentProvider
1471356840413   addons.manager  DEBUG   Provider finished startup: PreviousExperimentProvider
1471356840421   DeferredSave.extensions.json    DEBUG   Starting write
1471356840440   DeferredSave.addons.json    DEBUG   Starting write
1471356840460   DeferredSave.extensions.json    DEBUG   Write succeeded
1471356840495   DeferredSave.addons.json    DEBUG   Write succeeded

    at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewProfileExtensionConnection.java:112)
    ... 6 more

您需要将具有 marionette 能力的 DesiredCapabilities 传递给 trueFirefoxDriver 以及以下 :-

System.setProperty("webdriver.gecko.driver", "C:/Program Files/Mozilla Firefox/geckodriver-v0.8.0-win32/geckodriver.exe");

//Now you can Initialize marionette driver to launch firefox
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
WebDriver driver = new FirefoxDriver(capabilities);

已编辑 :如果不幸的是以上不起作用,我建议您将您的 firefox 降级到 45 或更低 并使用 FirefoxDriver 如下:-

WebDriver driver = new FirefoxDriver();

首先确保添加了所有 jar 文件,包括 /lib 文件夹中的文件,以下代码段将打开添加了 http 的浏览器 link。顺便说一下,我使用的是 win7 64bit,所以请留意 geckodrive 路径。

System.setProperty("webdriver.gecko.driver", "C:\Users\XYZ\Documents\geckodriver.exe");
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);

try {
    WebDriver driver = new FirefoxDriver();
    driver.navigate().to("http://google.com");
} catch(Exception e) {
    System.out.println(e);
}