它会在一个带有默认开始菜单的新窗口中打开,并显示一条通知,表明 Chrome 正在受到自动化测试软件的控制,但它不会转到该网址。
System.setProperty("webdriver.chrome.driver","C:\\Users\\"+System.getProperty("user.name")+"\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.setBinary("C:\\Users\\"+System.getProperty("user.name")+"\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe");
options.addArguments("--user-data-dir=C:\\Users\\"+System.getProperty("user.name")+"\\AppData\\Local\\Google\\Chrome\\User Data\\");
WebDriver driver = new ChromeDriver(options);
driver.get("https://www.google.com");
我也尝试过:
System.setProperty("webdriver.chrome.driver","C:\\Users\\"+System.getProperty("user.name")+"\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.setBinary("C:\\Users\\"+System.getProperty("user.name")+"\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe");
options.addArguments("--user-data-dir=C:\\Users\\"+System.getProperty("user.name")+"\\AppData\\Local\\Google\\Chrome\\User Data\\");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(capabilities);
driver.get("https://www.google.com");
它给出的异常(exception)
Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: crashed (Driver info: chromedriver=2.30.477700 (0057494ad8732195794a7b32078424f92a5fce41),platform=Windows NT 10.0.14393 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 61.65 seconds
我使用的是最新的 ChromeDriver 2.30 和 Selenium 3.4.0 版本
最佳答案
找到了我自己问题的答案。当我从配置文件路径复制默认文件夹并将其移至其他位置时,它起作用了。
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=C:/Users/"+System.getProperty("user.name")+"/Desktop/");
options.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(options);
driver.get("https://www.google.com");
关于java - Selenium ChromeDriver 不会使用默认用户数据来访问 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44875582/