它会在新窗口中打开,其中包含默认的开始菜单,并显示一条通知,指出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");
它给出的例外
线程"main"中的异常 org.openqa.selenium.WebDriver异常:未知错误:Chrome 无法启动:崩溃 (驱动程序信息:chromedriver = 2.30.477700 (0057494ad8732195794a7b32078424f92a5fce41(,platform=Windows NT 10.0.14393 x86_64((警告:服务器未提供任何堆栈跟踪信息( 命令持续时间或超时:61.65 秒
我使用的是最新的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");
试试这个:
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
System.setProperty("webdriver.chrome.driver", "CHROME_DRIVER_PATH");
capabilities.setCapability("chrome.switches", Arrays.asList("--no-default-browser-check"));
HashMap<String, String> chromePreferences = new HashMap<String, String>();
chromePreferences.put("profile.password_manager_enabled", "false");
capabilities.setCapability("chrome.prefs", chromePreferences);
ChromeOptions options = new ChromeOptions();
options.setBinary("CHROME_BINARY_PATH");
options.addArguments("--test-type");
options.addArguments("--allow-running-insecure-content");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = ChromeDriver(capabilities);
如果您不想移动配置文件,可以使用以下内容(将%Profile%
更改为要使用的配置文件(:
ChromeOptions options = new ChromeOptions();
options.addArguments("--user-data-dir=C:\Users\"+System.getProperty("user.name")+"\AppData\Local\Google\Chrome\User Data");
options.addArguments("--profile-directory=%Profile%")
options.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(options);
driver.get("https://www.google.com");