【问题标题】:Facing a HttpHostConnectException while using geckodriver使用 geckodriver 时遇到 HttpHostConnectException
【发布时间】:2017-09-13 02:16:21
【问题描述】:

我刚开始在我的办公室设置自动化 硒的帮助,但仅在初始水平上受到打击。我用了一个 基本代码最初只是为了检查浏览器是否工作和处理 代码进一步但遇到了问题。

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`

【问题讨论】:

  • 您尝试过其他网站吗?也许 google.com 拒绝连接。试试localhost

标签: java eclipse selenium webdriver geckodriver


【解决方案1】:

您的代码看起来不错,但有几点:

  1. 要使用 Selenium 3.x 和 Mozila Firefox 52.x,您需要从 this location 下载最新的 gecko 驱动程序并保存。

  2. 您需要提供“geckodriver.exe”的绝对路径,而不是“firefox.exe”

替换:

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

作者:

System.setProperty("webdriver.gecko.driver","C:\\your_directory\\geckodriver.exe");
  1. 最后你的代码看起来像:

    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");
    

这段代码对我来说执行得很好。

如果这对你有帮助,请告诉我。

【讨论】:

  • 感谢开发,但它仍然无法正常工作并引发此错误。线程“主”org.openqa.selenium.SessionNotCreatedException 中的异常:预期的浏览器二进制位置,但无法在默认位置找到二进制文件,没有提供“moz:firefoxOptions.binary”功能,并且在命令行上没有设置二进制标志(警告:服务器没有提供任何堆栈跟踪信息)命令持续时间或超时:0毫秒
  • @hemant 检查我更新的答案以及代码,如果可行,请告诉我。谢谢
猜你喜欢
  • 1970-01-01
  • 2012-05-27
  • 2015-03-14
  • 1970-01-01
  • 2023-03-27
  • 2015-05-22
  • 1970-01-01
  • 1970-01-01
  • 2012-02-21
相关资源
最近更新 更多