首页 > 解决方案 > 线程“主”org.openqa.selenium.WebDriverException 中的异常:java.net.ConnectException:无法连接到 localhost

问题描述

我只是想用 Selenium (3.14.0) 打开 Chrome。它适用于以下代码。

public class FirstClassForFirefox {    
public static void main(String[] args) {
 String projectLocation = System.getProperty("user.dir"); 
 System.setProperty("webdriver.chrome.driver",projectLocation+"/lib/chromedriver/chromedriver.exe");
 WebDriver driver = new ChromeDriver();
 driver.get("https://www.seleniumhq.org/");
 driver.quit();       
 }
}

另一方面,当我尝试使用以下代码打开 Firefox 浏览器时,发现了我的问题。

 public class FirstClassForFirefox {
 public static void main(String[] args) {

  String projectLocation = System.getProperty("user.dir");
  System.setProperty("webdriver.gecko.driver", projectLocation+"/lib/geckodriver/geckodriver-v0.23.0-win64.zip");
  System.setProperty("webdriver.firefox.bin", "C:\\Program Files\\Mozilla Firefox\\firefox.exe");
  WebDriver driver = new FirefoxDriver();
  driver.get("https://www.seleniumhq.org/");
  driver.quit();        
  }
}

我收到以下错误:-

Exception in thread "main" org.openqa.selenium.WebDriverException: java.net.ConnectException: Failed to connect to localhost/0:0:0:0:0:0:0:1:35765
Build info: version: '3.14.0', revision: 'aacccce0', time: '2018-08-02T20:13:22.693Z'
System info: host: 'HANEE-LAP', ip: '169.254.1.10', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_171'
Driver info: driver.version: FirefoxDriver
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:92)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:548)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:212)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:130)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:125)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:103)
    at test.FirstClassForFirefox.main(FirstClassForFirefox.java:16)
Caused by: java.net.ConnectException: Failed to connect to localhost/0:0:0:0:0:0:0:1:35765
    at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:242)
    at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:160)
    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)
    at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
    at okhttp3.RealCall.execute(RealCall.java:77)
    at org.openqa.selenium.remote.internal.OkHttpClient.execute(OkHttpClient.java:105)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:101)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:73)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:136)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    ... 6 more
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at okhttp3.internal.platform.Platform.connectSocket(Platform.java:129)
    at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:240)
    ... 28 more

请帮忙看看这有什么问题。可能是端口需要修改。谢谢!

标签: selenium-webdriverselenium-chromedriver

解决方案


推荐阅读