Issue Analysis
This issue can be reproduced even if you try to run chrome driver in two profile sequential without quiting the driver.
ChromeOptions chromeOptions1 = new ChromeOptions();
chromeOptions1.addArguments("--user-data-dir=C:/Users/My UserName/AppData/Local/Google/Chrome/User Data/Default");
chromeOptions1.addArguments("--profile-directory=Profile 1");
WebDriver driver1 = new ChromeDriver(chromeOptions1);
driver1.get("https://www.google.com");
ChromeOptions chromeOptions2 = new ChromeOptions();
chromeOptions2.addArguments("--user-data-dir=C:/Users/My UserName/AppData/Local/Google/Chrome/User Data/Default");
chromeOptions2.addArguments("--profile-directory=Profile 2");
WebDriver driver2 = new ChromeDriver(chromeOptions2);
When running first instance browser starts and page will be accesed. While running the second instance browser starts, but the page will not open.
The driver.get()
line fails with following exception for the second instance
Exception in thread "main" org.openqa.selenium.NoSuchSessionException: invalid session id
(Driver info: chromedriver=70.0.3538.16 (16ed95b41bb05e565b11fb66ac33c660b721f778),platform=Windows NT 10.0.17134 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 2.99 seconds
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{message=unknown error: Chrome failed to start: crashed
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location C:\Program Files (x86)\Google\Chrome\Application\chrome.exe is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
(Driver info: chromedriver=70.0.3538.16 (16ed95b41bb05e565b11fb66ac33c660b721f778),platform=Windows NT 10.0.17134 x86_64), platform=ANY}]
When the first instance is started the user data directory get locked and we are getting error for the second instance as the user data directory is in use.
We can simulate this issue by opening one chrome instance manually with one profile and trying to open one more chrome instance with other profile using chrome driver.
Solution
We have to use different user-data directory for each profile. We no need to create profile manually in chrome browser and also no need to provide --profile-directory
argument in chrome options. But you can maintain the sessions and history by mentioning different user-data-dir path for each chrome driver instance
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--user-data-dir=C:/ChromeProfiles/FirstProfile"); // Custom directory path for first profile
WebDriver driver = new ChromeDriver(chromeOptions);
driver.get("https://www.google.com");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--user-data-dir=C:/ChromeProfiles/SecondProfile"); // Custom directory path second profile
WebDriver driver = new ChromeDriver(chromeOptions);
driver.get("https://www.google.com");
This is will maintains the sessions and history in two profile that you are looking for.
Also multithreading will work without any issue.
class Driver extends Thread {
private String profile;
public Driver(String profile){
this.profile=profile;
}
public void run()
{
System.out.println ("Thread " +
Thread.currentThread().getId() +
" is running");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--no-sandbox");
chromeOptions.addArguments("--user-data-dir=C:/ChromeProfiles/"+profile);
// chromeOptions.addArguments("--profile-directory="+profile);
chromeOptions.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(chromeOptions);
driver.get("https://www.google.com");
}
}
public class MultiThreadDriver
{
public static void main(String[] args)
{
ChromeDriverManager.getInstance().setup();
Driver object = new Driver("First Profile");
object.start();
Driver object1 = new Driver("Second Profile");
object1.start();
}
}