gpt4 book ai didi

java - 使用 geckodriver 时遇到 HttpHostConnectException

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:50:43 25 4
gpt4 key购买 nike

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

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`

最佳答案

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

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

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

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

关于java - 使用 geckodriver 时遇到 HttpHostConnectException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43451877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com