1

它會在一個新窗口中打開,並顯示默認的開始菜單,並顯示Chrome正在通過自動化測試軟件控制的通知,但它不會轉到網址。Selenium ChromeDriver不會使用默認用戶數據訪問網址

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 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和硒3.4.0版本

+0

是您的谷歌瀏覽器58之間的版本 - 60? – Renato

+0

@renato是的,它是59 – coder35

+0

[Selenium Docker鏡像](https://github.com/SeleniumHQ/docker-selenium/blob/master/NodeChrome)設置爲「Chrome 53」和「ChromeDriver 2.30」。你可以嘗試版本'53'看看它是否有幫助嗎? – Tom

回答

1

找到了我自己問題的答案。當我從配置文件路徑中複製默認文件夾並將其放到其他位置並且沒有將路徑添加/默認添加到路徑中時它工作正常,因爲Chrome會添加它。

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

試試這個:

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

如果我添加了'options.addArguments(「 - user-data-dir = C:\\ Users \\」+ System.getProperty(「user.name」 )+「\\ AppData \\ Local \\ Google \\ Chrome \\ User Data \\」);' – coder35

0

如果您不想移動配置文件,可以使用下面的(改變%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");