Home > Enterprise >  How to fix selenium.WebDriverException: chrome not reachable
How to fix selenium.WebDriverException: chrome not reachable

Time:05-16

Not exactly as

I'm following Running Selenium Tests with ChromeDriver on Linux

and tried to run its demo, included below:

import java.io.File;
import java.io.IOException;
import java.net.*;

import org.junit.*;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public class ChromeRemoteDriver {
    
    public static void main(String []args) throws MalformedURLException{
        new DesiredCapabilities();
            URL serverurl = new URL("http://localhost:9515");
            DesiredCapabilities capabilities = DesiredCapabilities.chrome();
            WebDriver driver = new RemoteWebDriver(serverurl,capabilities);
        driver.get("http://www.google.com");
        WebElement searchEdit = driver.findElement(By.name("q"));
        searchEdit.sendKeys("Selftechy on google");
        searchEdit.submit();

    }
}

after I've started my local ChromeDriver:

/usr/local/bin/chromedriver &
[1] 27777

Starting ChromeDriver 101.0.4951.41 (93c720db8323b3ec10d056025ab95c23a31997c9-refs/branch-heads/4951@{#904}) on port 9515
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.

But when running the above demo, I'm getting:

May 13, 2022 11:51:26 PM org.openqa.selenium.remote.DesiredCapabilities chrome
Exception in thread "main" org.openqa.selenium.WebDriverException: chrome not reachable
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:53'

Why is that and how to fix?

CodePudding user response:

I have encoutered similar problem. I am running my Selenium tests locally and "webdriver exception chrome not reachable" error suddenly showed up.

The problem was that I already had too much tabs in my regular chrome browser. After getting frustrated I have closed few tabs and suddenly it worked. I am not sure if there is a certain limit of tabs, but you can give it a try.

CodePudding user response:

I recommend using docker for running your chromedriver, mainly because you stated you want headless execution. To do this:

  1. Download docker desktop https://www.docker.com/products/docker-desktop/

  2. run docker run -d -p 4444:4444 -p 5900:5900 --shm-size="2g" selenium/standalone-chrome:4.1.4-20220427 (this will download and start a standalone chromedriver in a docker container)

  3. Change your serverurl to URL serverurl = new URL("http:localhost:4444/wd/hub");

Complete main method:

    public static void main(String []args) throws MalformedURLException {
        new DesiredCapabilities();
        URL serverurl = new URL("http:localhost:4444/wd/hub");
        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        WebDriver driver = new RemoteWebDriver(serverurl,capabilities);
        driver.get("http://www.google.com");
        WebElement searchEdit = driver.findElement(By.name("q"));
        searchEdit.sendKeys("Selftechy on google");
        searchEdit.submit();
    }


  • More information on the subject can be found here: https://github.com/SeleniumHQ/docker-selenium

  • If you want to run the container on a port either than 4444 you will need to create your own docker image for chrome with EXPOSE value of the port you desire :-

how to create a selenium/standalone-chrome image with changeable port (the 4444)

https://github.com/SeleniumHQ/docker-selenium/blob/trunk/StandaloneChrome/Dockerfile

  • Related