java - 从 Selenium RemoteWebDriver 连接 Selenium Docker Firefox 容器时如何修复 "Connection Refused"错误?

标签 java selenium-remotedriver selenium-docker

我一直在尝试通过为 Standalone-Firefox 浏览器创建 Docker 容器并使用 Selenium RemoteWebDriver 连接它来学习 Docker。

我已经安装了 docker 镜像并确保它正在运行。我正在尝试访问 Google.com 并打印标题。

但是,我收到“线程“main”org.openqa.selenium.remote.UnreachableBrowserException 中出现异常:无法启动新 session 。可能的原因是远程服务器的地址无效或浏览器启动失败。”错误。

Docker 镜像已正确设置

https://github.com/SeleniumHQ/docker-selenium

![Docker 镜像运行成功](E:\Udemy Courses\SDET Architect\Successful Docker Running.JPG)

package dockerValidation;

import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

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

        URL url = new URL("http://localhost:4444/wd/hub");      
        // Static Class named DesiredCapabilities is used. No need to create object
        DesiredCapabilities capabilities = DesiredCapabilities.firefox();

        // RemoteWebDriver to run test on Cloud, Docker, etc
        RemoteWebDriver rwd = new RemoteWebDriver(url, capabilities);

        rwd.get("http://google.com");
        System.out.println(rwd.getTitle());
    }
}

预期结果(Eclipse 控制台)

May 15, 2019 6:36:39 PM org.openqa.selenium.remote.DesiredCapabilities firefox INFO: Using new FirefoxOptions() is preferred to DesiredCapabilities.firefox() May 15, 2019 6:36:55 PM org.openqa.selenium.remote.ProtocolHandshake createSession INFO: Detected Dialect: OSS Google

实际结果

May 15, 2019 6:36:39 PM org.openqa.selenium.remote.DesiredCapabilities firefox INFO: Using new FirefoxOptions() is preferred to DesiredCapabilities.firefox() 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: 'DESKTOP-QJQOV4L', ip: '192.168.99.1', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_131' 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.(RemoteWebDriver.java:131) at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:144) at dockerValidation.ChromeStandAloneTest.main(ChromeStandAloneTest.java:20) 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:589) at okhttp3.internal.platform.Platform.connectSocket(Platform.java:129) at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:245) ... 26 more

最佳答案

我也有同样的情况,重新启动 docker 对我有帮助。这不是最好的解决方案,但它有效。

关于java - 从 Selenium RemoteWebDriver 连接 Selenium Docker Firefox 容器时如何修复 "Connection Refused"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56159086/

相关文章:

selenium - 如何使用 Selenium::Remote::Driver 抑制 "warning"?

python - SessionNotCreatedException : Message: Unable to create new service: ChromeDriverService with Selenium 3. 14.0 和 Python 3.7.3

scala - Docker 上的 Selenium : Testing a Docker Image within the same `docker-compose` file

Windows 上的 Docker-for-selenium

java - method.invoke 上的参数数量错误

java - 我的计算器 undefined variable

java - java中私有(private)类中公共(public)方法的范围 - 无包

Java - 如何将通用参数作为 Class<T> 传递给构造函数