title: WebDriver Java: Reattach existing webdriver browser session to a driver instance
body: <p>I'm looking for a way to reattach an existing webdriver browser session to a driver instance so i could control the browser again.</p>
  
  <p>So far, I've tried the following:<br>
  <strong>I.</strong> ==========================================<br></p>
  
  <p>Reference: <a href="https://stackoverflow.com/a/38827934/2285470">https://stackoverflow.com/a/38827934/2285470</a></p>
  
  <p><strong>Browser:</strong> Firefox v. 51.01 / Chrome v. 56.0.2924.87</p>
  
  <p><strong>Driver:</strong> geckodriver v. 0.14 / chromedriver v. 2.27</p>
  
  <p><strong>Solution tried:</strong><br>
  1. Create a custom class that extends RemoteWebDriver</p>
  
  <pre><code>public class RemoteDriver extends RemoteWebDriver {
      public RemoteDriver(URL url, String sessionId) {
          super();
          setSessionId(sessionId);
          setCommandExecutor(new HttpCommandExecutor(url) {
              @Override
              public Response execute(Command command) throws IOException {
                  if (command.getName() != "newSession") {
                      return super.execute(command);
                  }
                  return super.execute(new Command(getSessionId(), "getCapabilities"));
              }
          });
          startSession(new DesiredCapabilities());
      }
  }
  </code></pre>
  
  <ol start="2">
  <li>Start a new firefox instance</li>
  <li>Get the session Id and store it somewhere (property file)</li>
  <li>Stop the test, leave the browser open</li>
  <li><p>Invoke the custom class that extends the RemoteWebDriver using the following:</p>
  
  <p><code>RemoteDriver cdriver = new RemoteDriver(new URL("http://localhost:7055/hub"),"my-session-id");</code></p></li>
  </ol>
  
  <p><strong>Result:</strong> <code>org.openqa.selenium.WebDriverException: No command or response codec has been defined. Unable to proceed</code> on the line <code>return super.execute(new Command(getSessionId(), "getCapabilities"));</code></p>
  
  <p><strong>II.</strong> ==========================================<br>
  <strong>Reference:</strong> <a href="https://groups.google.com/forum/#!topic/selenium-developers/1LygDvlQ3H4" rel="nofollow noreferrer">https://groups.google.com/forum/#!topic/selenium-developers/1LygDvlQ3H4</a></p>
  
  <p><strong>Browser:</strong> Firefox v. 51.01 / Chrome v. 56.0.2924.87</p>
  
  <p><strong>Driver:</strong> geckodriver v. 0.14 / chromedriver v. 2.27</p>
  
  <p><strong>Solution tried:</strong><br>
  1. Create a custom class that extends RemoteWebDriver</p>
  
  <pre><code>public class RemoteDriver extends RemoteWebDriver {
      @Override
      protected void startSession(Capabilities desiredCapabilities) {
          String sid = getSessionIdFromPropertyFile();
          if (sid != null) {
              setSessionId(sid);
              try {
                  super.startSession(desiredCapabilities);
              } catch (WebDriverException e) {
                  // session is not valid
                  sid = null;
              }
          }
          if (sid == null) {
              super.startSession(desiredCapabilities);
              saveSessionIdToPropertyFile(getSessionId().toString());
          }
      }
  }
  </code></pre>
  
  <ol start="2">
  <li>Start a new firefox instance</li>
  <li>Get the session Id and store it somewhere (property file)</li>
  <li>Stop the test, leave the browser open</li>
  <li><p>Invoke the custom class that extends the RemoteWebDriver using the following:</p>
  
  <p><code>RemoteDriver cdriver = new RemoteDriver();</code>
  <code>cdriver.startSession(DesiredCapabilities.firefox());</code>
  <code>cdriver.get("https://www.google.com");</code></p></li>
  </ol>
  
  <p><strong>Result:</strong> <code>org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.</code></p>
  
  <p>Seems others can get it to work but after a few hours of trying I still unable to do this. </p>
  
  <p>I'm using selenium-java version 3.1.0</p>
  
  <p>Thanks in advance.</p>