繁体   English   中英

Selenium java.net.SocketException:调用 driver.quit() 时连接重置;

[英]Selenium java.net.SocketException: Connection reset when calling driver.quit();

我看到了这些线程:

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

selenium/java- java.net.SocketException: 连接重置

但我仍然不明白这里的问题。 一切正常,我从网站上得到了响应和内容,完成后我调用了driver.quit(); / driver.close(); 并得到例外。 如果我不调用那个.quit(); 我没有得到例外。

获取 web 抓取内容后出现此异常:

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

那么这里的解决方案是什么? 我试图阅读这么多堆栈线程但没有找到任何解决方案

下面是我尝试过的相同代码,它工作正常,无一例外。 唯一的区别是我没有添加以下代码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();

}

这是控制台 output:

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

java.net.SocketException:连接重置

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();
  • 控制台 output:

     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

结论

以编程方式,我在您的代码块中没有看到任何错误。 但是请确保:


参考

您可以在以下位置找到一些相关的详细讨论:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM