繁体   English   中英

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

[英]org.openqa.selenium.WebDriverException: java.net.ConnectException: Failed to connect to localhost/0:0:0:0:0:0:0:1:1941 with GeckoDriver and Selenium

我正在尝试使用 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

请有人能帮我找出问题所在吗?

这个错误信息...

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服务。

虽然与硒3.x版工作,GeckoDriverFirefox启动新GeckoDriver服务和Firefox浏览器会话,你需要通过传递与设置为GeckoDriver绝对路径沿着webdriver.gecko.driver System.setProperty()行如下:

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

此外,

  • 您的JDK 版本1.8.0_161 ,这已经很旧了。
  • 您的Selenium 客户端版本是v3.141.59
  • 我们不知道您的GeckoDriver版本。
  • 我们不知道您的Firefox版本。

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

解决方案

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM