简体   繁体   中英

How to fix “Connection Refused” error while connecting with Selenium Docker Firefox container from Selenium RemoteWebDriver?

I have been trying to learn Docker by creating a Docker container for Standalone-Firefox browser and connecting it using Selenium RemoteWebDriver.

I have installed a docker image and made sure it is running. I am trying to access Google.com and print the title.

However, I am getting "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." error.

Docker Image is correctly setup

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

![Docker image is running successfully](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());
    }
}

Expected Result (Eclipse Console)

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

Actual Result

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.RealIntercepto rChain.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.A bstractPlainSocketImpl.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

I had the same, restarting docker helped me. Not the best solution, but it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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