硒负载铬配置文件java

时间:2020-05-02 07:33:41

标签: java selenium profile

我尝试将铬配置文件加载到硒中。但是,每当我上传个人资料时,都会出现错误:

invalid argument: user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use - user-data-dir.
String chromeProfile = "C:\\Users\\ad\\AppData\\Local\\Google\\Chrome\\User Data";
ChromeDriverService chSvc = new ChromeDriverService.Builder()
   .usingDriverExecutable(new File("C:\\Driver\\chromedriver.exe")).usingAnyFreePort().build();
ChromeOptions chOption = new ChromeOptions();
chOption.addArguments("--user-data-dir=" + chromeProfile);
chOption.addArguments("--profile-directory=Profile 33");
chOption.addArguments("--start-maximized");        
ChromeDriver driver = new ChromeDriver(chSvc, chOption);
driver.get("https://google.com");

enter image description here

1 个答案:

答案 0 :(得分:1)

您不能使用相同的user-data-dir运行多个ChromeDriver实例。您可以做的是每次创建ChromeDriver实例时,都创建一个临时目录,然后在ChromeOptions chOption.addArguments("--user-data-dir=" + tempDir);

中进行设置