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

调用driver.quit()出现java.net.SocketException: Connection reset的解决办法

解决Selenium调用driver.quit()/close()时的Connection reset异常

爬取流程完全正常,能成功获取网站响应与内容,但调用driver.quit();或driver.close();时会抛出java.net.SocketException: Connection reset异常,不调用.quit();则无异常。已查阅相关Stack Overflow线程但未找到解决方法。

异常信息

2023-01-31T21:28:31.222+01:00  WARN 8400 --- [cHttpClient-1-4] o.a.netty.handler.WebSocketHandler       : onError

java.net.SocketException: Connection reset
    at java.base/sun.nio.ch.SocketChannelImpl.throwConnectionReset(SocketChannelImpl.java:394) ~[na:na]
    at java.base/sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:426) ~[na:na]
    at io.netty.buffer.PooledByteBuf.setBytes(PooledByteBuf.java:259) ~[netty-buffer-4.1.87.Final.jar:4.1.87.Final]
    at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1132) ~[netty-buffer-4.1.87.Final.jar:4.1.87.Final]
    at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:357) ~[netty-transport-4.1.87.Final.jar:4.1.87.Final]
    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:151) ~[netty-transport-4.1.87.Final.jar:4.1.87.Final]
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:788) ~[netty-transport-4.1.87.Final.jar:4.1.87.Final]
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:724) ~[netty-transport-4.1.87.Final.jar:4.1.87.Final]
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:650) ~[netty-transport-4.1.87.Final.jar:4.1.87.Final]
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:562) ~[netty-transport-4.1.87.Final.jar:4.1.87.Final]
    at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997) ~[netty-common-4.1.87.Final.jar:4.1.87.Final]
    at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[netty-common-4.1.87.Final.jar:4.1.87.Final]
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-common-4.1.87.Final.jar:4.1.87.Final]
    at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]

2023-01-31T21:28:31.229+01:00  WARN 8400 --- [cHttpClient-1-4] o.openqa.selenium.remote.http.WebSocket  : Connection reset

java.net.SocketException: Connection reset
    at java.base/sun.nio.ch.SocketChannelImpl.throwConnectionReset(SocketChannelImpl.java:394) ~[na:na]
    at java.base/sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:426) ~[na:na]
    at io.netty.buffer.PooledByteBuf.setBytes(PooledByteBuf.java:259) ~[netty-buffer-4.1.87.Final.jar:4.1.87.Final]
    at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1132) ~[netty-buffer-4.1.87.Final.jar:4.1.87.Final]
    at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:357) ~[netty-transport-4.1.87.Final.jar:4.1.87.Final]
    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:151) ~[netty-transport-4.1.87.Final.jar:4.1.87.Final]
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:788) ~[netty-transport-4.1.87.Final.jar:4.1.87.Final]
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:724) ~[netty-transport-4.1.87.Final.jar:4.1.87.Final]
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:650) ~[netty-transport-4.1.87.Final.jar:4.1.87.Final]
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:562) ~[netty-transport-4.1.87.Final.jar:4.1.87.Final]
    at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997) ~[netty-common-4.1.87.Final.jar:4.1.87.Final]
    at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[netty-common-4.1.87.Final.jar:4.1.87.Final]
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-common-4.1.87.Final.jar:4.1.87.Final]
    at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]

核心代码

String url = "https://relatedwords.org/relatedto/";
System.setProperty("webdriver.chrome.driver", filePath);

ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");

WebDriver driver = new ChromeDriver(chromeOptions);
driver.get(url + "Fishsticks");

WebElement words = driver.findElement(By.className("words"));
final List<WebElement> wordList = words.findElements(By.tagName("a"));
wordList.forEach(word -> System.out.println(word.getText()));

// 去掉这行就不会抛出异常
driver.quit();

环境信息

  • Chrome版本:109.0.5414.120(无更新)
  • ChromeDriver版本:109.0.5414.74
  • Selenium版本:4.8.0

解决方案

1. 匹配Chrome与ChromeDriver的精确版本

当前Chrome版本为109.0.5414.120,但ChromeDriver用的是109.0.5414.74,版本号后缀不一致会导致通信异常。下载与Chrome完全匹配版本的ChromeDriver替换现有文件即可。

2. 添加ChromeOptions参数优化配置

在初始化ChromeOptions时添加以下参数,避免WebSocket连接异常:

chromeOptions.addArguments("--remote-debugging-port=9222");
chromeOptions.addArguments("--disable-dev-shm-usage");
chromeOptions.addArguments("--no-sandbox");
chromeOptions.addArguments("--disable-gpu"); // 无头模式下推荐添加

3. 使用Selenium 4内置的驱动管理

放弃手动设置System.setProperty,改用WebDriverManager自动管理驱动版本,确保驱动与浏览器版本匹配:

// 需先引入WebDriverManager依赖
WebDriverManager.chromedriver().setup();

ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");

WebDriver driver = new ChromeDriver(chromeOptions);

4. 临时捕获异常(不推荐长期使用)

如果以上方法暂时无法生效,可以捕获driver.quit()时的异常,避免程序中断:

try {
    driver.quit();
} catch (Exception e) {
    // 忽略Connection reset异常
}

5. 检查网络环境

确认本地防火墙、代理软件没有拦截ChromeDriver与Chrome之间的WebSocket连接,同时检查本地9222等调试端口是否被其他程序占用。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 02:56:52