如果另一个chrome实例打开,Selenium chromedriver将不会启动URL

SOA*_*thm 3 selenium

我尝试使用selenium weDriver加载chrome配置文件.配置文件加载正常,但在尝试加载URL时失败.

我注意到,当有另一个chrome实例打开时,无论它是否由webDriver打开,都会发生此问题.我有硒2.53.1.

System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=C:/Users/useName/AppData/Local/Google/Chrome/User Data");
options.addArguments("--start-maximized");
driver = new ChromeDriver(options);

driver.get("www.google.com") // here is where it fails. It works fine if I close all chrome browsers before I run the test
Run Code Online (Sandbox Code Playgroud)

SOA*_*thm 8

我找到了解决此问题的方法.我注意到这个问题的发生是因为如果有另一个使用相同配置文件的打开实例,则chromedriver将无法使用相同的配置文件启动.例如,如果chrome.exe已使用默认配置文件打开,则chromedriver.exe将无法启动默认配置文件,因为chrome.exe已打开并使用相同的配置文件.

要解决此问题,您需要通过复制默认配置文件为自动创建单独的配置文件,以便chromedriver.exe和chrome.exe不共享相同的默认配置文件.

默认的chrome配置文件位于以下位置:

C:\ Users\yourUserName\AppData\Local\Google\Chrome\User Data \

将所有文件从User Data文件夹复制到新文件夹,并将其命名为AutomationProfile

将文件复制到新文件夹后,您可以将其用于脚本.

        String userProfile= "C:\\Users\\YourUserName\\AppData\\Local\\Google\\Chrome\\AutomationProfile\\";
        ChromeOptions options = new ChromeOptions();
        options.addArguments("user-data-dir="+userProfile);
        options.addArguments("--start-maximized");

        driver = new ChromeDriver(options);
Run Code Online (Sandbox Code Playgroud)

确保在测试结束时使用driver.quit(),这样就不会使chromedriver.exe保持打开状态