java - 在 Docker 中运行 Selenium 测试时出现连接被拒绝的 UnreachableBrowserException

标签 java docker selenium selenium-webdriver selenium-chromedriver

我正在尝试在 Docker 中运行以下简单的 Selenium 测试(使用 Docker 工具箱安装在 Window10 Home 中)。 我收到了 UnreachableBrowserException。 下面是我尝试在 Docker 容器中运行的测试。

    URL url=new URL("http://localhost:4444/wd/hub");
    DesiredCapabilities dc=DesiredCapabilities.chrome();
    RemoteWebDriver rDr=new RemoteWebDriver(url,dc);
    rDr.get("https://www.google.com");
    System.out.println(rDr.getTitle());
    rDr.quit();

下面是我收到的错误。

INFO: Using `new ChromeOptions()` is preferred to `DesiredCapabilities.chrome()`
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: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: '', ip: '', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_241'
Driver info: driver.version: RemoteWebDriver
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:573)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:213)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:131)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:144)
    at com.dockerz.MyDocker.ChromeStandAloneTest.main(ChromeStandAloneTest.java:16)
Caused by: java.net.ConnectException: Failed to connect to localhost/0:0:0:0:0:0:0:1:4444
    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.RemoteWebDriver.execute(RemoteWebDriver.java:552)
    ... 4 more
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:606)
    at okhttp3.internal.platform.Platform.connectSocket(Platform.java:129)
    at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:245)
    ... 26 more

enter image description here

最佳答案

尝试 System.setProperty("java.net.preferIPv4Stack", "true");

回收自stackoverflow

另请查看下面的资源,其中包含可供使用的实际示例 RemoteWebDriver

希望有帮助。如果您发现这有用,请标记为答案。谢谢

关于java - 在 Docker 中运行 Selenium 测试时出现连接被拒绝的 UnreachableBrowserException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61503150/

相关文章:

java - 为什么组合器输出记录 = 0?

docker - 本地 Gitlab cicd 使用 linux runner 失败 'fatal: unable to access...Could not resolve host:...'

java - 具有多个条件的 xpath 将包含 'a' 和 'div' 标记

python - 在 Raspberry Pi 4 上使用 Chromedriver 运行 Selenium 的问题

docker - Google Cloud Run 安全问题

java - 我需要帮助创建一个函数,该函数将从 Java (Selenium) 中的 CSV 文件读取(打开新的浏览器选项卡)url

java - 如何用接口(interface)序列化一个类?

c# - 尝试从 URL 读取标记器文件时斯坦福 POS 标记器出现错误

java - 错误: Selection does not contain a main type; answers to similar questions not working

windows - 手动删除 Hyper-V 中的 MobyLinuxVM 后,Windows 10 上的 Docker 无法启动