Selenium (webdriver) study notes--ChromeDriver

It is easy to start firefox with webdriver, the following code will do.

WebDriver driver = new FirefoxDriver();
driver.get("http://www.baidu.com");
driver.close();
  • 1
  • 2
  • 3

But launching the Google Chrome browser is more complicated.

If the following code is still used, an error will be reported.

WebDriver driver = new ChromeDriver();
driver.get("http://www.baidu.com");
driver.close();
  • 1
  • 2
  • 3

Error message:

Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see http://code.google.com/p/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://chromedriver.storage.googleapis.com/index.html
    at com.google.common.base.Preconditions.checkState(Preconditions.java:197)
    at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:110)
    at org.openqa.selenium.chrome.ChromeDriverService.access$000(ChromeDriverService.java:32)
    at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:118)
    at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:291)
    at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:82)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:117)
    at webdrivers.UseChromeDriver.main(UseChromeDriver.java:9)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

Solution: Download chromedriver

Download address:  https://sites.google.com/a/chromium.org/chromedriver/downloads 
Please check your version of Google Chrome to download the corresponding chromedriver. By 
the way, there is one more thing, the picture below shows all the chromedrivers of a certain version , There is no win64, I have tested the 
win64 operating system with win32. 
chromedriver 
After downloading, unzip it and get the chromedriver. The code changes are as follows:

System.setProperty("webdriver.chrome.driver", "D:/drivers/chromedriver_win32-2.14/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.baidu.com");
driver.close();
  • 1
  • 2
  • 3
  • 4

reason:

ChromeDriver is a standalone server developed in cooperation with the Chromium team. 
It consists of three parts, the chrome browser, the language bindings provided by the Selenium project ("the driver") (I don't know what this is, I guess it's selenium-chrome-driver-*.jar), and the executable The chromedriver is missing from the previous program. This executable chromedriver is a bridge to the other two.

The server requires the chrome browser to be installed in the default path

YOU installation path
Linux /usr/bin/google-chrome
Mac /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome
windows7 C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
Windows XP %HOMEPATH%\Local Settings\Application Data\Google\Chrome\Application\chrome.exe

For Linux systems, /usr/bin/google-chrome is the symlink of the real installation path.

Reprinted from: https://blog.csdn.net/qiyueqinglian/article/details/47419209

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325498025&siteId=291194637