title: Attach ChromeDriver to existing istance (Selenium): Unknown error cannot connect to chrome at localhost:9222
body: <p>I'm struggling in attaching Chromedriver to an existing Chrome session; the idea is to work always on a specific chrome separate session istance which I use only for this application. I found some similar questions but didn't helped: please note driver and chrome versions correctly match.</p>
  <p>Opening Chrome with batch file:</p>
  <pre><code>chrome.exe --user-data-dir=&quot;C:\Users\aless\AppData\Local\Google\Chrome\UserDataDebug2&quot; -–remote-debugging-port=9222
  </code></pre>
  <p>Initializing ChromeDriver:</p>
  <pre><code>//Please note: it works fine without the ChromOptions, but it will open a new istance at every run of the application, forcing me to repeat the login in the website. That's why I want to work always in the same profile/session (UserDataDebug2) and connect always to that.
  
  System.setProperty(&quot;webdriver.chrome.driver&quot;,&quot;chromedriver/chromedriver.exe&quot;);
  ChromeOptions option = new ChromeOptions();
  option.setExperimentalOption(&quot;debuggerAddress&quot;,&quot;localhost:9222&quot;);
  DriverManager driverManager = new DriverManager(new ChromeDriver(option));
  </code></pre>
  <p>DriverManager class:</p>
  <pre><code>// Not really important: I hold here the reference to the driver just in order to have all functions which use the driver in this class
  public class DriverManager {    
  
  private WebDriver driver;
  JavascriptExecutor js;
  
  public DriverManager(WebDriver driver) {
       this.driver = driver;
       js = (JavascriptExecutor) driver;  
  }
  </code></pre>
  <p>Console Error output:</p>
  <pre><code>SLF4J: Failed to load class &quot;org.slf4j.impl.StaticLoggerBinder&quot;.
  SLF4J: Defaulting to no-operation (NOP) logger implementation
  SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
  Starting ChromeDriver 100.0.4896.60 (6a5d10861ce8de5fce22564658033b43cb7de047-refs/branch-heads/4896@{#875}) on port 63484
  Only local connections are allowed.
  Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
  ChromeDriver was started successfully.
  Exception in thread &quot;main&quot; org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 500. Message: unknown error: cannot connect to chrome at localhost:9222
  from chrome not reachable
  Build info: version: '4.1.2', revision: '9a5a329c5a'
  System info: host: 'ALESURFACE', ip: '', os.name: 'Windows 11', os.arch: 'amd64', os.version: '10.0', java.version: '17.0.2'
  Driver info: org.openqa.selenium.chrome.ChromeDriver
  Command: [null, newSession {capabilities=[Capabilities {browserName: chrome, goog:chromeOptions: {args: [], debuggerAddress: localhost:9222, extensions: []}}], desiredCapabilities=Capabilities {browserName: chrome, goog:chromeOptions: {args: [], debuggerAddress: localhost:9222, extensions: []}}}]
      at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:126)
      at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:84)
      at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:62)
      at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:156)
      at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:167)
      at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:142)
      at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:558)
      at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:245)
      at org.openqa.selenium.remote.RemoteWebDriver.&lt;init&gt;(RemoteWebDriver.java:161)
      at org.openqa.selenium.chromium.ChromiumDriver.&lt;init&gt;(ChromiumDriver.java:108)
      at org.openqa.selenium.chrome.ChromeDriver.&lt;init&gt;(ChromeDriver.java:106)
      at org.openqa.selenium.chrome.ChromeDriver.&lt;init&gt;(ChromeDriver.java:93)
      at org.openqa.selenium.chrome.ChromeDriver.&lt;init&gt;(ChromeDriver.java:82)
      at main.main(main.java:21)
  </code></pre>