Java.net.SocketException: 在Selenium driver.close()或driver.quit()语句中连接重置

3
我经常在使用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

1
使用最新的chromedriver和chrome(114.0.5735.90)以及Selenium 4.10.0,在这里遇到了相同的错误。 - undefined
请参阅答案:https://stackoverflow.com/questions/72540689/how-to-help-this-org-openqa-selenium-remote-http-websocketlistener-onerror,这对我有用。 - undefined
3个回答

0
你是否在使用驱动程序监听器? 如果是的话,那么即使退出了,驱动程序仍然会被调用,从而导致错误。

如果你有新的问题,请点击提问按钮进行提问。如果有需要提供背景信息,请附上此问题的链接。- 来自审核 - undefined

-1
从异常的名字可以看出,你的chromedriver和chrome浏览器之间存在连接问题。
升级Chrome浏览器至84版本,然后重试,或者你也可以下载与你的Chrome版本兼容的降级版chromedriver。

我在几个页面上看到了这个建议,并相应地升级了我的Chrome驱动程序。但是即使我的浏览器和Chrome驱动程序是兼容的版本(都是84版本),异常仍然发生。 还有其他什么我可以检查或修改以避免这个异常吗? - undefined
请尝试以下两个检查,并告诉我是否解决了问题:
  1. 检查一下你的chromedriver是否没有关闭。我认为使用driver.quit()应该不会出现这个问题。
  2. 如果仍然出现错误,请检查项目中使用的selenium-java版本。确保是最新的稳定版本。
- undefined
在driver.quit()中没有遇到这个问题,但在driver.close()中遇到了这个问题,我也有最新的驱动程序。 - undefined

-2

我也遇到了同样的错误,但在更改Selenium版本后问题得到了解决。


你的回答可以通过添加额外的支持信息来改进。请[编辑]以添加更多细节,如引用或文档,以便他人可以确认你的回答是正确的。您可以在帮助中心中找到有关如何编写良好答案的更多信息。 - undefined
这并没有真正回答问题。如果你有其他问题,可以点击提问来提出。如果你想在这个问题有新的回答时收到通知,你可以关注此问题。一旦你拥有足够的声望,你还可以添加悬赏以吸引更多关注。- 来自评论 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接