多线程:chromedriver无法在第二个窗口中打开url [英] multithreading: chromedriver does not open url in second window

查看:179
本文介绍了多线程:chromedriver无法在第二个窗口中打开url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

线程功能中的Java代码:

Java code in thread function:

    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");

创建对象并使用以下代码在线程中启动

and create object and start in thread with following code

    Driver d1 = new Driver(profile);
    d1.start();

    Driver d2 = new Driver(profile1);
    d1.start();

已创建了两个不同的配置文件,代码在单线程中运行良好,但在多线程中无法在两个单独的窗口中打开google网站.它说,

two different profiles have been created, code works well with single thread but with multiple threads it does not open google website in two separate windows. it says,

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.由其他线程打开的窗口保持空闲状态.谁能帮忙吗?

and it opens google in only one window. window opened by anther thread remains idle. Could anyone please help?

推荐答案

问题分析

即使您尝试按两个配置文件顺序运行chrome驱动程序而不退出驱动程序,也可以重现此问题.

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);

运行首次实例时,浏览器将启动,并且将进入页面.运行第二个实例浏览器时,它将启动,但该页面将无法打开. driver.get()行失败,第二个实例出现以下异常

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.

我们可以通过手动使用一个配置文件打开一个chrome实例并尝试使用chrome驱动程序尝试使用其他配置文件打开另一个chrome实例来模拟此问题.

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.

解决方案

我们必须为每个配置文件使用不同的用户数据目录.我们无需在chrome浏览器中手动创建配置文件,也无需在chrome选项中提供--profile-directory参数.但是您可以通过为每个chrome驱动程序实例提及不同的user-data-dir路径来维护会话和历史记录

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();
    }
}

这篇关于多线程:chromedriver无法在第二个窗口中打开url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆