Selenium自动化出现WebSocket$Listener onError连接重置异常求助
Selenium自动化测试Connection reset异常排查
异常信息
Jul 27, 2023 11:20:18 AM org.openqa.selenium.remote.http.WebSocket$Listener onError WARNING: Connection reset java.net.SocketException: Connection reset at java.base/sun.nio.ch.SocketChannelImpl.throwConnectionReset(SocketChannelImpl.java:394) at java.base/sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:426) at io.netty.buffer.PooledByteBuf.setBytes(PooledByteBuf.java:258) at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1132) at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:357) at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:151) at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:722) at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:658) at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:584) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:496) at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997) at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) at java.base/java.lang.Thread.run(Thread.java:833)
问题背景
已添加ChromeOptions配置 options.addArguments("--remote-allow-origins=*");,但仍触发上述连接重置异常,需排查可能原因。
代码片段
WebDriverManager.chromedriver().setup(); ChromeDriverManager.getInstance(CHROME).setup(); ChromeOptions options = new ChromeOptions(); options.addArguments("--remote-allow-origins=*"); WebDriver driver = new ChromeDriver(options);
可能的原因及排查方向
- 重复初始化WebDriverManager:代码中同时调用
WebDriverManager.chromedriver().setup()和ChromeDriverManager.getInstance(CHROME).setup(),会导致驱动版本冲突或重复配置,引发连接异常。保留其中一行即可,推荐用WebDriverManager.chromedriver().setup(),它会自动匹配Chrome版本。 - Chrome与ChromeDriver版本不兼容:即使WebDriverManager自动管理版本,也可能出现匹配偏差。手动检查Chrome版本(chrome://settings/help),指定对应版本的ChromeDriver测试,示例:
WebDriverManager.chromedriver().driverVersion("116.0.5845.96").setup(); - WebSocket连接被拦截或中断:Selenium 4+依赖WebSocket与浏览器通信,网络不稳定、浏览器进程意外崩溃,或系统防火墙/杀毒软件拦截WebSocket端口,都会导致连接重置。可尝试关闭杀毒软件/防火墙,或添加
--disable-web-security、--no-sandbox参数测试。 - 残留Chrome进程占用资源:之前的测试结束后,Chrome或Chromedriver进程未完全关闭,残留进程占用端口导致新连接失败。测试前手动结束所有相关进程,或在代码中确保执行
driver.quit()销毁进程。 - 系统资源不足:内存或CPU占用过高时,Chrome/Chromedriver进程被系统强制终止,引发连接重置。关闭其他高资源占用程序,增加JVM内存参数(如
-Xmx2G)测试。
内容的提问来源于stack exchange,提问作者CtrlAltTest
相关产品推荐
相关产品推荐

