multithreading - 多线程:chromedriver 不在第二个窗口中打开 url
问题描述
线程函数中的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();
已经创建了两个不同的配置文件,代码在单线程中运行良好,但对于多线程,它不会在两个单独的窗口中打开谷歌网站。它说,
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.
它只在一个窗口中打开谷歌。花药线程打开的窗口保持空闲。有人可以帮忙吗?
解决方案
问题分析
即使您尝试在两个配置文件中连续运行 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 浏览器中手动创建配置文件,也无需--profile-directory
在 chrome 选项中提供参数。但是您可以通过为每个 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();
}
}
推荐阅读
- javascript - Firebase 身份验证错误 - 每当我尝试使用谷歌登录时
- python - 如何从python中字符串类型的字典键中获取整数?
- python - Skit-learn with Spacy parallelization error with RandomizedSearchCV
- javascript - How to get values from two textboxes and display on third on button click using Javascript?
- assembly - How to disassemble fasm-generated binary?
- c++ - 是防止部分读取原子存储所必需的“memory_order_relaxed”
- mysql - Use subgroups in databases
- r - 从 R 中不可用的 URL 加载数据集,仅从本地加载:设置问题?
- angular - Creating volume in custom Docker image on GCP causing error
- javascript - 由按钮控制的可折叠 HTML 表格,带有多个具有不同数据集的子表格