从Selenium RemoteWebDriver与Selenium Docker Firefox容器连接时如何解决“拒绝连接”错误?

时间:2019-05-15 23:57:23

标签: java selenium-remotedriver selenium-docker

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

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

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

Docker Image已正确设置

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控制台)

  

2019年5月15日下午6:36:39 org.openqa.selenium.remote.DesiredCapabilities firefox   信息:使用new FirefoxOptions()优于DesiredCapabilities.firefox()   2019年5月15日下午6:36:55 org.openqa.selenium.remote.ProtocolHandshake createSession   INFO:检测到的方言:OSS   谷歌

实际结果

  

2019年5月15日下午6:36:39 org.openqa.selenium.remote.DesiredCapabilities firefox   信息:使用new FirefoxOptions()优于DesiredCapabilities.firefox()   线程“主”中的异常org.openqa.selenium.remote.UnreachableBrowserException:无法启动新会话。可能的原因是远程服务器的地址无效或浏览器启动失败。   构建信息:版本:'3.141.59',修订版本:'e82be7d358',时间:'2018-11-14T08:17:03'   系统信息:主机:'DESKTOP-QJQOV4L',ip:'192.168.99.1',操作系统名称:'Windows 10',os.arch:'amd64',os.version:'10 .0',java.version:'1.8 .0_131'   驱动程序信息:driver.version:RemoteWebDriver       在org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:573)       在org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:213)       在org.openqa.selenium.remote.RemoteWebDriver。(RemoteWebDriver.java:131)       在org.openqa.selenium.remote.RemoteWebDriver。(RemoteWebDriver.java:144)       在dockerValidation.ChromeStandAloneTest.main(ChromeStandAloneTest.java:20)   引起原因:java.net.ConnectException:无法连接到localhost / 0:0:0:0:0:0:0:0:1:4444       在okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:247)       在okhttp3.internal.connection.RealConnection.connect(RealConnection.java:165)       在okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:257)       在okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:135)       在okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:114)       在okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)       在okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)       在okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)       在okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)       在okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)       在okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)       在okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)       在okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)       在okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126)       在okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)       在okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)       在okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)       在okhttp3.RealCall.execute(RealCall.java:77)       在org.openqa.selenium.remote.internal.OkHttpClient.execute(OkHttpClient.java:103)       在org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:105)       在org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:74)       在org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:136)       在org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)       ...另外4个   引起原因:java.net.ConnectException:连接被拒绝:connect       在java.net.DualStackPlainSocketImpl.waitForConnect(本机方法)       在java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)       在java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)       在java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)       在java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)       在java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)       在java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)       在java.net.Socket.connect(Socket.java:589)       在okhttp3.internal.platform.Platform.connectSocket(Platform.java:129)       在okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:245)       ...另外26个

1 个答案:

答案 0 :(得分:0)

我也一样,重启docker帮助了我。不是最好的解决方案,但是可以。