Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facing a HttpHostConnectException while using geckodriver

i just started setting up automation in my office with the help of selenium but strucked at the initial level only. I used a basic code initially just to check the browser is working and processing the code further but got an issue.

import java.io.IOException;
import java.net.InetAddress;
import java.util.concurrent.TimeUnit;

import org.apache.http.conn.HttpHostConnectException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;

public class ABC {




public static void main(String[] args) 
{

    System.setProperty("webdriver.gecko.driver","C:\\Users\\12345678\\
    AppData\\Local\\Mozilla Firefox\\firefox.exe");

    DesiredCapabilities dc=DesiredCapabilities.firefox();
    FirefoxProfile profile = new FirefoxProfile();
    dc.setCapability(FirefoxDriver.PROFILE, profile);
    WebDriver driver =  new FirefoxDriver(dc);
    driver.get("http://www.google.com");
    driver.manage().timeouts().implicitlyWait(10L, TimeUnit.SECONDS);

}`


    **Below is the error i am facing**

`

 Exception in thread "main" org.openqa.selenium.WebDriverException: 
    org.apache.http.conn.HttpHostConnectException: Connect to 
    localhost:44853 
    [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection 
    refused: connect
    Build info: version: '3.3.1', revision: '5234b32', time: '2017-03-10 
    09:04:52 -0800'
    System info: host: 'NODHCMSLTP1115', ip: '10.203.124.34', os.name: 
    'Windows 
    7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_92'
    Driver info: driver.version: FirefoxDriver at 

 org.openqa.selenium.remote.service.DriverCommandExecutor.execute(Driver
 CommandExecutor.java:91) at 




    org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.
     java:604)at 

  org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver
 .java:244)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>
    (RemoteWebDriver.java:131)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>
   (FirefoxDriver.java:218)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>
   (FirefoxDriver.java:125)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>
   (FirefoxDriver.java:150)
    at seleniumProjectauto.Rks_Asu.main(Rks_Asu.java:27)
    Caused by: org.apache.http.conn.HttpHostConnectException: Connect to 
    localhost:44853 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: 
    Connection refused: connect
    at 
    org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect
    (DefaultHttpClientConnectionOperator.java:158)
    at 
    org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect
    (PoolingHttpClientConnectionManager.java:353)
    at 
    org.apache.http.impl.execchain.MainClientExec.establishRoute
    (MainClientExec.java:380)
    at         
    org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.
    java:236)
    at 

   org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:
    184)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
    at 
    org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:
    110)
    at 
   org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttp
    Client
   .java:184)
    at 
    org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttp
    Client.java:71)
    at 
    org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttp
    Client.java:55)
    at org.openqa.selenium.remote.internal.ApacheHttpClient.fallBackExecute
    (ApacheHttpClient.java:142)
    at   
    org.openqa.selenium.remote.internal.ApacheHttpClient.execute(ApacheHttp
    Client.java:88)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(Protocol
    Handshake.java:296)
    at 
    org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHand
    shake.java:113)
    at 
    org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommand
    Executor.java:141)

at 

org.openqa.selenium.remote.service.DriverCommandExecutor.execute
(DriverCommandExecutor.java:82)
... 7 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(AbstractPlainSocket
Impl.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 
org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket
(PlainConnectionSocketFactory.java:74)
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect
(DefaultHttpClientConnectionOperator.java:141)
... 22 more`
like image 255
hemant Avatar asked May 24 '26 01:05

hemant


1 Answers

Your code looks fine but a couple of points:

  1. To work with Selenium 3.x & Mozila Firefox 52.x you need to download the latest gecko driver from this location and save it.

  2. Instead of "firefox.exe" you need to provide absolute path of the "geckodriver.exe"

Replace:

System.setProperty("webdriver.gecko.driver","C:\\Users\\12345678\\
    AppData\\Local\\Mozilla Firefox\\firefox.exe");

By:

System.setProperty("webdriver.gecko.driver","C:\\your_directory\\geckodriver.exe");
  1. Finally you code will look like:

    System.setProperty("webdriver.gecko.driver", "C:\\SeleniumUtilities\\BrowserDrivers\\geckodriver.exe");
    DesiredCapabilities dc = DesiredCapabilities.firefox();
    FirefoxProfile profile = new FirefoxProfile();
    dc.setCapability(FirefoxDriver.PROFILE, profile);
    WebDriver driver =  new FirefoxDriver(dc);
    driver.manage().window().maximize();
    driver.get("http:\\gmail.com");
    

This code executes good with me.

Let me know if this helps you.

like image 94
undetected Selenium Avatar answered May 26 '26 15:05

undetected Selenium



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!