几乎每次使用 driver.close() 或 driver.quit() 语句时,我都会看到 java.net.SocketException: Connection Reset。浏览器确实关闭了,并且到目前为止,该异常并未影响我的任何测试,但我仍然想了解为什么会发生这种情况。
我检查了版本不兼容,但即使在更新 ChromeDriver 后问题仍然存在。 (我将 ChromeDriver 84.0.4147.30 与 Google Chrome 版本 84.0.4147.105 一起使用。)
该问题是间歇性的,但通常在 80% 左右的时间发生。
代码:
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com/");
driver.close();
System.exit(0);
}
错误:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Starting ChromeDriver 84.0.4147.30 (48b3e868b4cc0aa7e8149519690b6f6949e110a8-refs/branch-heads/4147@{#310}) on port 24445
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Aug 07, 2020 6:39:11 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Aug 07, 2020 6:39:14 PM 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:345)
at java.base/sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:376)
at io.netty.buffer.PooledByteBuf.setBytes(PooledByteBuf.java:253)
at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1133)
at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:350)
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:148)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:714)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:650)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:576)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
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:832)
Process finished with exit code 0
这是一年后的事了,但我会定期使用 chrome v116 和 v116“用于测试”chrome 驱动程序(这是唯一能够尝试使用此版本的 chrome 的版本)收到此消息,并获取错误概要通过OP。 无法选择与公司发布和强制要求的任何其他版本的 Chrome 一起运行。
但是,测试无论如何都会完成。
正如异常名称所示,您的 chromedriver 和 chrome 浏览器之间的连接存在问题。
将chrome浏览器升级到84然后重试,或者您也可以下载与您的chrome版本兼容的降级版本的chromedriver。
我也遇到了同样的错误,但在更改 selenium 版本后解决了。