Home > Software engineering >  Selenium java.net.SocketException: Connection reset when calling driver.quit();
Selenium java.net.SocketException: Connection reset when calling driver.quit();

Time:02-01

I saw these threads:

java.net.SocketException: Connection reset on Selenium driver.close() or driver.quit() statements

selenium/java- java.net.SocketException: Connection reset

But I still don't understand the issue here. Everything is working fine, I get the response and content from the website and when I'm done I call the driver.quit(); / driver.close(); and get the exception. If I don't call that .quit(); I don't get the exception.

I get this exception after I get the web scraping content:

2023-01-31T21:28:31.222 01:00  WARN 8400 --- [cHttpClient-1-4] o.a.netty.handler.WebSocketHandler       : one rror

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]

This is the code from the main:

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

I use the latest chrome version I just checked, and there are no updates available (Version 109.0.5414.120) and I am using chrome driver version ChromeDriver 109.0.5414.74 and the latest selenium version 4.8.0

So what is the solution here? I tried to read so many stack threads but have not found any solution

CodePudding user response:

Below is the same code I have tried and it works fine with no exceptions. Only difference is that I have not added following code System.setProperty("webdriver.chrome.driver", filePath);. You do not need to set property when you are using the latest version of selenium(which you are using anyways). Try removing this line and see if it resolves your issue.

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

}

Here is the console 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

CodePudding user response:

java.net.SocketException: Connection reset

java.net.SocketException may occur due to several possible reasons as follows:

  • It can occur on the server-side when the client closed the socket connection before the response could be returned over the socket. For example, by quitting the browser before the response was retrieved.
  • It can also occur by writing to a connection that the other end has already closed normally resulting in an application protocol error.

This usecase

I took your program and executed it. Here is the result:

  • Code block:

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

Conclusion

Programmatically, I don't see any error in your code block. However ensure that:


References

You can find a couple of relevant detailed discussions in:

  • Related