0

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?

xpt
  • 20,363
  • 37
  • 127
  • 216

2 Answers2

0

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.

  • Thanks for the reply Sarah. I was trying to run it headless on the remote server. Would you double-check if my code or my process is correct please, since you are at least able to get similar things going, whereas I'm clueless where my problem is. Thanks. PS. after re-reading your post I think it might be the _"trying to run it headless on the remote server"_ part that is the problem. How to turn the code as headless, do you know pls? thx. – xpt May 15 '22 at 13:22
0

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

Nebisis
  • 1
  • 1
  • Thanks Nebisis. Welcome aboard. Just that I've follow your 3 steps word for word, but it is still not working. Have you tried it yourself sir? I got "Exception in thread "main" org.openqa.selenium.ScriptTimeoutException: Unable to execute request for an existing session: java.util.concurrent.TimeoutException" on my end. I then looked into https://www.selenium.dev/documentation/webdriver/remote_webdriver/, and found that the calling to `new RemoteWebDriver` is totally different than your code. – xpt May 16 '22 at 14:36