title: Selenium with ChromeDriver and using Chrome Profile
body: <p>I'm trying to validate if selenium-chromedriver can share cookies between multiple webdriver instances. The idea is that, I'll create one webdriver instance and login into the application. I'll keep this webdriver instance running and will create another webdriver instance and try to access a secure page on the same site. Since I already logged into the  application from first instance, I should be automatically logged into the second instance. But this didn't work. After a lot of research, I found that I need to set a specify the directory where Chrome creates the session cookies while creating the chromedriver instance. Following is the code I have.</p>
  
  <pre><code>public class TestClass {
  
     private static WebDriver webDriver = null;
  
     public static void main(String[] args) throws InterruptedException {
  
          TestClass tc = new TestClass();
  
          if(webDriver == null) {
  
              webDriver = tc.getWebDriverInstance();
  
              webDriver.get("https://example.com/loginpage");
              //enter userid/password, click login button
              //login is successful and redirected to next page - https://example.com/securepage
           }
  
           WebDriver newWebDriverOne = this.getWebDriverInstance();
           newWebDriverOne.get("https://example.com/securepage"); // this doesn't work
  
           WebDriver newWebDriverTwo = this.getWebDriverInstance();
           newWebDriverTwo.get("https://example.com/securepage");// this doesn't work
    }
  
    WebDriver getWebDriverInstance(){    
  
      DesiredCapabilities dCaps = DesiredCapabilities.chrome();    
      ChromeOptions options = new ChromeOptions();    
      options.addArguments("user-data-dir=C:/user/me/selenium/chrome");    
      dCaps.setCapability(ChromeOptions.CAPABILITY, options);
      return new ChromeDriver(dCaps); 
     }
   }
  </code></pre>
  
  <p>Problem with this is, when I call get() on the newWebDriver instance which is created after the first one, nothing happens. Selenium opens the second window as since I created a new webdriver instance, but get() doesn't do anything. I tried manually entering the securepage url in the opened window and it worked. I was able to see the secure page without getting redirected to login page. </p>
  
  <p>It seems like it is impossible to have multiple webdriver instances if we specify user-data-dir. Is there any other option to share cookie data between sessions?</p>
  
  <p>----- Update ------
  The reason I'm trying to do this is a very peculiar usecase. </p>
  
  <p>--> I need to run multiple automation runs simultaneously - only way I know to achieve this is creating multiple webdriver instances.</p>
  
  <p>--> Automation script needs to login using only one account. And the IDP allows only one active session at a time. That means, if the automation script logs in second webdriver instance, then the first webdriver instance logs out.</p>
  
  <p>So while researching a way to share session between webdriver instances, I came across user-data-dir option.</p>