Selenium Java无故抛出套接字异常(警告)

u5rb5r59  于 2023-03-16  发布在  Java
关注(0)|答案(1)|浏览(206)

我在Selenium的onError方法中收到一个“警告”,在Java应用程序中使用它(使用Spring批处理框架),我无法检测它抛出这个错误的原因和内容。
当我使用ChromeDriver类的quit()方法时会产生这个异常。当我尝试调试时,这个异常没有被抛出。

Starting ChromeDriver 110.0.5481.77 (65ed616c6e8ee3fe0ad64fe83796c020644d42af-refs/branch-heads/5481@{#839}) on port 58642
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
[1678747030.900][WARNING]: virtual void DevToolsClientImpl::AddListener(DevToolsEventListener *) subscribing a listener to the already connected DevToolsClient. Connection notification will not arrive.
[2m2023-03-13 23:37:10.946[0;39m [32m INFO[0;39m [35m15264[0;39m [2m---[0;39m [2m[ null to remote][0;39m [36mo.o.selenium.remote.ProtocolHandshake   [0;39m [2m:[0;39m Detected dialect: W3C
[2m2023-03-13 23:37:10.993[0;39m [32m INFO[0;39m [35m15264[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mo.o.selenium.devtools.CdpVersionFinder  [0;39m [2m:[0;39m Found exact CDP implementation for version 110
[2m2023-03-13 23:38:33.789[0;39m [32mDEBUG[0;39m [35m15264[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mo.s.b.item.database.JdbcBatchItemWriter [0;39m [2m:[0;39m Executing batch with 1 items.
[2m2023-03-13 23:39:19.811[0;39m [32mDEBUG[0;39m [35m15264[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mo.s.b.item.database.JdbcBatchItemWriter [0;39m [2m:[0;39m Executing batch with 1 items.
[2m2023-03-13 23:39:19.856[0;39m [33m WARN[0;39m [35m15264[0;39m [2m---[0;39m [2m[cHttpClient-1-4][0;39m [36mo.a.netty.handler.WebSocketHandler      [0;39m [2m:[0;39m 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:258) ~[netty-buffer-4.1.79.Final.jar:4.1.79.Final]
    at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1132) ~[netty-buffer-4.1.79.Final.jar:4.1.79.Final]
    at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:357) ~[netty-transport-4.1.79.Final.jar:4.1.79.Final]
    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:151) ~[netty-transport-4.1.79.Final.jar:4.1.79.Final]
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:722) ~[netty-transport-4.1.79.Final.jar:4.1.79.Final]
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:658) ~[netty-transport-4.1.79.Final.jar:4.1.79.Final]
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:584) ~[netty-transport-4.1.79.Final.jar:4.1.79.Final]
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:496) ~[netty-transport-4.1.79.Final.jar:4.1.79.Final]
    at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997) ~[netty-common-4.1.79.Final.jar:4.1.79.Final]
    at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[netty-common-4.1.79.Final.jar:4.1.79.Final]
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-common-4.1.79.Final.jar:4.1.79.Final]
    at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]

我的代码从ChromeDriver:

System.setProperty("webdriver.chrome.driver", "src\\main\\resources\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless=new");
ChromeDriver chromeDriver = new ChromeDriver(options);
            
chromeDriver.get("https://www.google.com/");
//select web items, interact with them
chromeDriver.quit();
e4yzc0pl

e4yzc0pl1#

每当你得到一个java.net.SocketException: Connection reset错误消息,这意味着插座两端之间的连接发生了一些事情。这是关于当你在网上看视频,然后你改变你的WiFi网络-你得到一个类似的错误。
更具体地说,对于您的问题,您在Web Socket仍然打开的情况下退出。在调用quit()之前,您必须首先终止ChromeDriver与浏览器之间的Web套接字连接。
您的问题不是您声称的“警告”。“警告”实际上不是问题的一部分,但它们仍然有价值。实际错误如下:

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:258) ~[netty-buffer-4.1.79.Final.jar:4.1.79.Final]
    at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1132) ~[netty-buffer-4.1.79.Final.jar:4.1.79.Final]
    at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:357) ~[netty-transport-4.1.79.Final.jar:4.1.79.Final]
    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:151) ~[netty-transport-4.1.79.Final.jar:4.1.79.Final]
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:722) ~[netty-transport-4.1.79.Final.jar:4.1.79.Final]
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:658) ~[netty-transport-4.1.79.Final.jar:4.1.79.Final]
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:584) ~[netty-transport-4.1.79.Final.jar:4.1.79.Final]
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:496) ~[netty-transport-4.1.79.Final.jar:4.1.79.Final]
    at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997) ~[netty-common-4.1.79.Final.jar:4.1.79.Final]
    at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[netty-common-4.1.79.Final.jar:4.1.79.Final]
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-common-4.1.79.Final.jar:4.1.79.Final]
    at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]

......但之前的警告告诉我两件事:
1.您正在尝试使用Chrome浏览器打开重复的连接
1.第一连接未终止。
chrome驱动程序和浏览器之间有两个连接,当调用quit()时,Selenium必须关闭它与浏览器的连接,而且这种情况只发生一次。
您可以在调用quit()之前手动关闭这两个连接(我不知道如何操作),或者您可以检查代码(或使用调试器)以查找第二个连接试图建立的位置。

相关问题