You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决Selenium执行Firefox测试时的localhost连接失败异常?

解决Selenium FirefoxDriver连接失败(ConnectException)的问题

先梳理下你的环境信息:

  • Firefox版本:60.0(64位)
  • Selenium版本:3.12.0
  • Gecko Driver版本:v0.20.1-win64
  • Java版本:1.8.0

Chrome测试正常,但Firefox完全无法启动,还抛出了java.net.ConnectException: Failed to connect to localhost/127.0.0.1:4243的错误。我整理了几个针对性的排查和解决步骤,你可以逐一尝试:

1. 确认版本兼容性匹配

你的Firefox 60属于较旧版本,虽然GeckoDriver 0.20.1标称支持Firefox 55+,但和Selenium 3.12.0的组合可能存在适配bug。建议:

  • 若想保留Firefox 60,尝试降级GeckoDriver到v0.19.1(这个版本对旧版Firefox的兼容性更稳定)
  • 或者升级Firefox到Firefox 68 ESR版本,它和Selenium 3.12.0、GeckoDriver 0.20.1的兼容性会更好

2. 显式指定Firefox二进制路径

有时候系统默认的Firefox路径没被识别,或者存在多版本冲突,你可以在初始化Driver时手动指定Firefox的安装路径:

System.setProperty("webdriver.gecko.driver", "你的geckodriver.exe路径");
FirefoxOptions options = new FirefoxOptions();
// 替换成你实际的Firefox安装路径
options.setBinary("C:\\Program Files\\Mozilla Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver(options);

3. 检查端口占用情况

报错里提到的4243端口可能被其他进程占用,导致GeckoDriver无法绑定。你可以:

  • 打开命令提示符,执行netstat -ano | findstr :4243查看占用端口的进程ID
  • 结束该进程,或者让GeckoDriver使用自定义未占用的端口:
System.setProperty("webdriver.gecko.driver", "你的geckodriver.exe路径");
FirefoxDriverService service = new FirefoxDriverService.Builder()
    .usingPort(4567) // 替换成未被占用的端口
    .build();
WebDriver driver = new FirefoxDriver(service);

4. 禁用Firefox自动更新

旧版Firefox如果自动更新,会直接破坏原本的版本兼容性。你可以:

  • 打开Firefox后输入about:preferences#general
  • 在“Firefox 更新”区域选择“从不检查更新(不推荐)”

5. 排查防火墙/杀毒软件拦截

Windows防火墙或第三方杀毒软件可能会阻止GeckoDriver和Firefox之间的通信,你可以临时关闭它们,再运行测试验证是否解决问题。

附上你提供的错误日志供参考:

Exception in thread "main" org.openqa.selenium.WebDriverException: java.net.ConnectException: Failed to connect to localhost/127.0.0.1:4243 Build info: version: '3.12.0', revision: '7c6e0b3', time: '2018-05-08T15:15:03.216Z' System info: host: 'COMPNAME', ip: '192.168.78.32', 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:543) at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:207) at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:130) at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:125) at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:103) at testCase2.main(testCase2.java:17) Caused by: java.net.ConnectException: Failed to connect to localhost/127.0.0.1:4243 at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:240) at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:158) at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:256) at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:134) at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:113) 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:125) 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:125) at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:238) ... 28 more

内容的提问来源于stack exchange,提问作者jaspearson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 06:50:57