0

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       : 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]

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

Maremare
  • 1
  • 3
  • AFAIK, "connection reset" means the other side already closed the connection for you. In HTTP, it's normal close the connection immediately after all data is sent (unless "keep alive" is set?), so what you're getting is probably normal. – markspace Jan 31 '23 at 20:44
  • @markspace Okay, so what should I do about it? What I understand from this is that I should not call the quit(), is that a better approach to this issue, or better to catch it and do nothing about it? What do you think? – Maremare Jan 31 '23 at 21:02

2 Answers2

0

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
Shawn
  • 4,064
  • 2
  • 11
  • 23
  • Do you mean you executed your program with out the `System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");` line? – undetected Selenium Jan 31 '23 at 22:04
  • Yes @undetectedSelenium – Shawn Jan 31 '23 at 22:17
  • And without setting any EnvVar/Path or any dependency per se? – undetected Selenium Jan 31 '23 at 22:19
  • That's correct. It will all be handled by the library Selenium Manager packaged within selenium version 4.6.0 and above. The fact that you are so curiously asking is making me rethink. :) However am certain about what I have mentioned. I am 100% sure the above code in my answer worked without System.setProperty :) – Shawn Jan 31 '23 at 22:30
  • Great point :) well we are still struggling to keep up the show with the initial support of _Selenium Manager_. Brownie points on your way ;) – undetected Selenium Jan 31 '23 at 22:37
  • I tried to remove it after dragging the chrome driver to the project and the program worked, but I still got the connection exceptions though, so I guess there is something wrong on my computer side – Maremare Feb 01 '23 at 09:59
0

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:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I downloaded JDK 8u351, and when I check for java installations on my pc I got these: - Java SE Development Kit 8 Update 351 - Java SE Development Kit 8 Update 333 - Java(TM) SE Development Kit 18.0.2.1 Update 351 - Java 8 Update 351 Which of these can I uninstall to have only the latest? @undetected – Maremare Feb 01 '23 at 10:19
  • _JDK 8u351_ is the latest, even though _JDK 8u333_ would also suffice. – undetected Selenium Feb 01 '23 at 10:37