【问题标题】:multithreading: chromedriver does not open url in second window多线程:chromedriver 不在第二个窗口中打开 url
【发布时间】:2018-10-13 21:09:41
【问题描述】:

线程函数中的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.

它只在一个窗口中打开谷歌。花药线程打开的窗口保持空闲。有人可以帮忙吗?

【问题讨论】:

  • 你能用("--user-data-dir="+config.chromeUserDir)("--profile-directory="+profile) 所指的内容更新问题吗?
  • config.chromeUserDir 是 Linux 上的 ~/.config/google-chrome/ , --profile-directory="+profile 是配置文件的目录,即在下创建的“配置文件 1”或“配置文件 2” ~/.config/google-chrome/ 当我们在谷歌浏览器上创建个人资料时。

标签: multithreading selenium google-chrome selenium-chromedriver


【解决方案1】:

问题分析

即使您尝试在两个配置文件中连续运行 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();
    }
}

【讨论】:

  • 您是否通过多次运行代码来测试此解决方案? ~/.config/google-chrome 下已经为每个配置文件创建了两个不同的目录。我通过在主目录下创建不同的配置文件目录来尝试您的代码,不知何故它曾经工作过一次,但后来即使在为配置文件创建新目录后它也不起作用。每次我想使用现有的 chrome 配置文件进行测试时。我可能创建了 10 个配置文件,我想同时在 10 个配置文件上运行自动化。
  • 感谢您的分析...您的修复工作有效,但我无法为您的答案投票.. :(
  • 如果我有一个包含多个配置文件的用户数据目录,如何将其拆分为每个配置文件的多个用户数据?
【解决方案2】:

由于第一个实例运行时用户数据目录被锁定,您可以更改配置文件 2 的 forlder 缓存。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-20
    • 1970-01-01
    • 2012-12-21
    • 2021-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-06
    相关资源
    最近更新 更多