我一直在尝试通过为Standalone-Firefox浏览器创建一个Docker容器并使用Selenium RemoteWebDriver连接它来学习Docker。
我已经安装了一个 docker 映像并确保它正在运行。我正在尝试访问 Google.com 并打印标题。
但是,我收到"线程"main"org.openqa.selenium.remote.UnreachableBrowser异常:无法启动新会话。可能的原因是远程服务器的地址无效或浏览器启动失败。
码头工人映像已正确设置
https://github.com/SeleniumHQ/docker-selenium
![码头工人镜像运行成功](e:\Udemy Courses\SDET Architect\Success 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 信息:检测到的方言:OSS 谷歌
实际结果
2019年5月15日 下午6:36:39 org.openqa.selenium.remote.DesiredCapabilities firefox 信息:使用
new FirefoxOptions()
优于DesiredCapabilities.firefox()
线程"main"org.openqa.selenium.remote.UnreachableBrowserException中的异常:无法启动新会话。可能的原因是远程服务器的地址无效或浏览器启动失败。 内部版本信息:版本:"3.141.59",修订版:"e82be7d358",时间:"2018-11-14T08:17:03" 系统信息: 主机: '桌面-QJQOV4L', ip: '192.168.99.1', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_131' 驱动程序信息: 驱动程序版本: 远程网络驱动程序 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.(远程网络驱动程序.java:131) at org.openqa.selenium.remote.RemoteWebDriver.(远程网络驱动程序.java:144) at dockerValidation.ChromeStandAloneTest.main(ChromeStandAloneTest.java:20) 导致:java.net.Connect异常:无法连接到本地主机/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) 在 okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200) 在 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 更多 导致:java.net.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 个
我也有同样的情况,重新启动 docker 帮助了我。不是最好的解决方案,但它有效。