JRichardsz
JRichardsz

Reputation: 16574

Selenium Chrome DevTools: Unknown error: cannot connect to chrome at localhost:6666 from chrome not reachable

I'm trying to execute my selenium automation in a chrome which is already open.

I follow these steps: https://medium.com/@harith.sankalpa/connect-selenium-driver-to-an-existing-chrome-browser-instance-41435b67affd

I open a chrome with this line:

/opt/google/chrome/chrome --remote-debugging-port=6666

This is my java code

System.setProperty("webdriver.chrome.driver", "/../chromedriver");
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("debuggerAddress","localhost:6666");
// Initialize browser
WebDriver driver = new ChromeDriver(options);
// Open Google
driver.get("http://www.google.com");
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys("jrichardsz");    
// Close browser
driver.close();

When I run this code I get this log:

Starting ChromeDriver 84.0.4147.30 (48b3e868c0aa7e814951969b4c0b6f6949e110a8-refs/branch-heads/4147@{#310}) on port 12155
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.

And after some seconds I get: chrome not reachable error

Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: cannot connect to chrome at localhost:6666
from chrome not reachable
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'jane_doe', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '***', java.version: '1.8.0_151'
Driver info: driver.version: ChromeDriver
remote stacktrace: #0 0x556168a87ea9 <unknown>

    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.openqa.selenium.remote.W3CHandshakeResponse.lambda$errorHandler$0(W3CHandshakeResponse.java:62)
    at org.openqa.selenium.remote.HandshakeResponse.lambda$getResponseFunction$0(HandshakeResponse.java:30)
    at org.openqa.selenium.remote.ProtocolHandshake.lambda$createSession$0(ProtocolHandshake.java:126)
    at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
    at java.util.Spliterators$ArraySpliterator.tryAdvance(Spliterators.java:958)
    at java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:126)
    at java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:498)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:485)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
    at java.util.stream.FindOps$FindOp.evaluateSequential(FindOps.java:152)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.util.stream.ReferencePipeline.findFirst(ReferencePipeline.java:464)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:128)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:74)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:136)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:213)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:131)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:181)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:168)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:157)
    at OpenedChrome.main(OpenedChrome.java:24)

I tried also with chromiun and opera and I get the same error.

My chrome is opened but java cannot connect to it.

I reviewed these links unlucky

Upvotes: 3

Views: 12100

Answers (2)

JRichardsz
JRichardsz

Reputation: 16574

On my case this was due because I was using a port (6666) to start my chrome in debug mode, which is restricted according to : restricted [Unsafe] ports list on Chrome

6665,    // Alternate IRC [Apple addition]
6666,    // Alternate IRC [Apple addition]
6667,    // Standard IRC [Apple addition]

A complete chrome list is here and here. Firefox allowed ports here. Also how to force to allow some ports, here

Steps to identify the error

  • If I startup my chrome in debug mode at port wxyz. I need to validate if this port is correctly used by chrome or just if this port is listening.
  • Port listening could be validated using telnet localhost wxyz or in windows with nc -zvv localhost wxyz
  • I found another option to validate if chrome has started successfully using this port. It was a kind of http like http://localhost:wxyz/json/version. If port is listening, a json response is received. If due to some issue like not allowed ports, chrome is not picking this port, http://localhost:wxyz/json/version returns an error. If I found the real endpoint I will update this answer.

If port is listening correctly, another low level error is the reason:

  • Just check your java code to point to exact port.
  • Driver exact version. Selenium is very sensitive to the drivers version.
  • Used browser version compatible with driver version.
  • Try on linux. In windows some unexpected errors appear.

Upvotes: 1

Rahul L
Rahul L

Reputation: 4349

You can set below property to check the debug logs

System.setProperty("webdriver.chrome.verboseLogging", "true");

Example for Windows OS:

If any existing chrome process is running and you have open new instance of chrome using

> chrome.exe --remote-debugging-port=9222

then you may get below logs while executing the selenium script in question

[DEBUG]: DevTools HTTP Request: http://localhost:9222/json/version
[DEBUG]: DevTools HTTP Request failed

To open the clean instance of chrome browser for debugging

chrome.exe --remote-debugging-port=9222 --no-first-run --no-default-browser-check --user-data-dir="D:\remotedebugfolder"

Now run the selenium with

options.setExperimentalOption("debuggerAddress","localhost:9222");

Upvotes: 1

Related Questions