org.openqa.selenium.WebDriverException:java.net.ConnectException:无法使用GeckoDriver和Selenium连接到localhost / 0:0:0:0:0:0:0:1:11:1941

时间:2019-02-09 20:47:37

标签: java selenium firefox geckodriver selenium-firefoxdriver

我正在尝试使用Eclipse编写一个简单的自动化测试,并且在设置它时遇到了问题。我无法启动Firefox。我的代码在下面,然后是错误堆栈。

代码:

package cucumberTest;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class seleniumTest {

    private static WebDriver driver = null;

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        //https://www.toolsqa.com/cucumber/first-cucumber-selenium-java-test/
        // new instance of frefox driver
        driver = new FirefoxDriver();

         //Put a Implicit wait, this means that any search for elements on the page 
         // could take the time the implicit wait is set for before throwing exception

        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        //open page
         driver.get("http://www.store.demoqa.com");

错误堆栈:这是我尝试在ECLIPSE中运行代码时在控制台窗口中显示的内容。

Exception in thread "main" org.openqa.selenium.WebDriverException: java.net.ConnectException: Failed to connect to localhost/0:0:0:0:0:0:0:1:1941
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:53'
System info: host: 'LAPTOP-P3SB6R8A', ip: '192.168.1.6', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_161'
Driver info: driver.version: FirefoxDriver
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:92)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:213)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:131)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:147)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:125)
    at cucumberTest.seleniumTest.main(seleniumTest.java:18)
Caused by: java.net.ConnectException: Failed to connect to localhost/0:0:0:0:0:0:0:1:1941
    at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:247)
    at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:165)
    at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:257)
    at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:135)
    at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:114)
    at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
    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:126)
    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:103)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:105)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:74)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:136)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    ... 6 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 okhttp3.internal.platform.Platform.connectSocket(Platform.java:129)
    at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:245)
    ... 28 more

请有人可以帮助我确定问题所在吗?

1 个答案:

答案 0 :(得分:0)

此错误消息...

Exception in thread "main" org.openqa.selenium.WebDriverException: java.net.ConnectException: Failed to connect to localhost/0:0:0:0:0:0:0:1:1941
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:53'
System info: host: 'LAPTOP-P3SB6R8A', ip: '192.168.1.6', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_161'
Driver info: driver.version: FirefoxDriver
.
Caused by: java.net.ConnectException: Failed to connect to localhost/0:0:0:0:0:0:0:1:1941

...表示您的程序无法启动/产生新的 GeckoDriver 服务。

在使用 Selenium v​​3.x GeckoDriver Firefox 来启动新的 GeckoDriver 服务和< em> Firefox浏览器会话,您需要传递 Key webdriver.gecko.driver 以及设置为 Value GeckoDriver 通过System.setProperty()行的>绝对路径,如下所示:

System.setProperty("webdriver.gecko.driver", "C:\\path\\to\\geckodriver.exe");
driver = new FirefoxDriver();

另外,

  • 您的 JDK版本 1.8.0_161 ,它已经很旧了。
  • 您的 Selenium Client 版本是 v3.141.59
  • 您的 GeckoDriver 版本对我们未知
  • 您的 Firefox 版本对我们来说是未知

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

解决方案

  • JDK 升级到最新级别JDK 8u202
  • GeckoDriver 升级到GeckoDriver v0.24.0级。
  • GeckoDriver位于指定位置。
  • GeckoDriver对非root用户具有可执行权限。
  • Firefox 版本升级到 Firefox v65.0 级别。
  • 通过您的 IDE
  • 清理您的项目工作区重建您的项目,并且仅具有必需的依赖项。
  • 如果您的基本 Web客户端版本过旧,请通过Revo Uninstaller进行卸载,并安装最新版本的 Web客户端。 li>
  • 进行系统重启
  • 以非root用户身份执行Test
  • 始终在driver.quit()方法内调用tearDown(){},以优雅地关闭和销毁 WebDriver Web Client 实例。