线程功能中的Java代码:
System.setProperty("webdriver.chrome.driver", "/usr/bin/chromedriver");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--no-sandbox");
chromeOptions.addArguments("--user-data-dir="+config.chromeUserDir);
chromeOptions.addArguments("--profile-directory="+profile);
chromeOptions.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(chromeOptions);
driver.get("https://www.google.com");
创建对象并使用以下代码在线程中启动
Driver d1 = new Driver(profile);
d1.start();
Driver d2 = new Driver(profile1);
d1.start();
已创建了两个不同的配置文件,代码在单线程中运行良好,但在多线程中无法在两个单独的窗口中打开google网站。它说,
Starting ChromeDriver 2.42.591071 (0b695ff80972cc1a65a5cd643186d2ae582cd4ac) on port 25692
Only local connections are allowed.
Starting ChromeDriver 2.42.591071 (0b695ff80972cc1a65a5cd643186d2ae582cd4ac) on port 25954
Only local connections are allowed.
Oct 14, 2018 2:10:46 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Oct 14, 2018 2:10:46 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Created new window in existing browser session.
,它仅在一个窗口中打开google。由其他线程打开的窗口保持空闲状态。谁能帮忙吗?
答案 0 :(得分:2)
问题分析
即使您尝试按两个配置文件顺序运行chrome驱动程序而不退出驱动程序,也可以重现此问题。
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);
在运行首次实例时,浏览器将启动,并且将进入页面。运行第二个实例浏览器时,它将启动,但该页面将无法打开。
driver.get()
行失败,第二个实例出现以下异常
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}]
第一个实例启动时,用户数据目录被锁定,而第二个实例在使用用户数据目录时出现错误。
我们可以通过手动使用一个配置文件打开一个chrome实例并尝试使用chrome驱动程序尝试使用其他配置文件打开另一个chrome实例来模拟此问题。
解决方案
我们必须为每个配置文件使用不同的用户数据目录。我们无需在chrome浏览器中手动创建配置文件,也无需在chrome选项中提供--profile-directory
参数。但是您可以通过为每个chrome驱动程序实例提及不同的user-data-dir路径来维护会话和历史记录
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");
这将在您要查找的两个配置文件中保留会话和历史记录。
多线程也可以正常工作。
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();
}
}
答案 1 :(得分:0)
因为在第一个实例运行时用户数据目录被锁定,所以您可以更改配置文件2的forll缓存。