Java.NET中出现异常:调用driver.quit()时连接复位;

xkftehaa  于 2023-02-02  发布在  Java
关注(0)|答案(2)|浏览(164)

我看到了这些线索:
java.net.SocketException: Connection reset on Selenium driver.close() or driver.quit() statements
selenium/java- java.net.SocketException: Connection reset
但我还是不明白这里的问题。一切都很好,我从网站上得到了响应和内容,当我完成时,我调用driver.quit();/driver.close();并得到了异常。如果我不调用.quit();,我就得不到异常。
我得到这个异常后,我得到了网页抓取内容:

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()));

        // when I remove this I do not get any exceptions
        driver.quit();

我使用的是刚刚检查的最新chrome版本,没有可用的更新(版本109.0.5414.120),我使用的是chrome驱动程序版本ChromeDriver 109.0.5414.74和最新的selenium版本4.8.0
那么这里的解决方案是什么呢?我尝试读取这么多堆栈线程,但没有找到任何解决方案

7eumitmz

7eumitmz1#

下面是我试过的相同代码,它工作正常,没有例外。唯一的区别是我没有添加下面的代码System.setProperty("webdriver.chrome.driver", filePath);。当您使用最新版本的selenium(无论如何,您都在使用)时,您不需要设置属性。请尝试删除此行,看看它是否解决了您的问题。

public static void main(String[] args){
    
    ChromeOptions options = new ChromeOptions();
    options.addArguments("--headless");

    WebDriver driver = new ChromeDriver(options);
    driver.get("https://relatedwords.org/relatedto/" + "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();

}

以下是控制台输出:

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 109.0.5414.74 (e7c5703604daa9cc128ccf5a5d3e993513758913-refs/branch-heads/5414@{#1172}) on port 2732
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
batter
cod
united kingdom
ketchup
herring
whitefish
hake
haddock
pollock
refrigerator
british english
breaded
american english
europe
food processing
supermarket
grilling
tartar sauce
fish
machine
backbone
parents
frozen food
wheel
blade
television commercial
southampton
shallow frying
skin
breadcrumbs
salmon
bone
deep frying
world war ii
gorton's of gloucester
clarence birdseye
store brand
vegetable oil
jgzswidk

jgzswidk2#

java. net套接字异常:连接复位

java.net.SocketException可能由于以下几个可能的原因而发生:

  • 当客户端在通过套接字返回响应之前关闭套接字连接时,可能会在服务器端发生这种情况。例如,在检索响应之前退出浏览器。
  • 如果写入另一端已正常关闭的连接,也会发生这种情况,从而导致应用程序协议错误。

此用例

我把你的程序执行了一下,结果如下:

  • 代码块:
System.setProperty("webdriver.chrome.driver", "C:\\BrowserDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(options);
driver.get("https://relatedwords.org/relatedto/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();
  • 控制台输出:
batter
cod
united kingdom
ketchup
herring
whitefish
hake
haddock
pollock
refrigerator
british english
breaded
american english
europe
food processing
supermarket
grilling
tartar sauce
fish
machine
backbone
parents
frozen food
wheel
blade
television commercial
southampton
shallow frying
skin
breadcrumbs
salmon
bone
deep frying
world war ii
gorton's of gloucester
clarence birdseye
store brand
vegetable oil

结论

从程序上看,我没有看到你的代码块中有任何错误。但是请确保:

参考文献

您可以在以下内容中找到一些相关的详细讨论:

相关问题