160

I tried this

WebDriver driver = new ChromeDriver();

But I'm getting the error as

Failed tests: setUp(com.TEST): The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see code here . The latest version can be downloaded from this link

How can I make Chrome test the Selenium WebDriver test cases?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gnik
  • 7,120
  • 20
  • 79
  • 129
  • It would be helpful if you can tell, whether you have downloaded the selenium chrome driver from http://code.google.com/p/chromedriver/downloads/list and added it to library in eclipse under "Add External Jars" before running your selenium-java code. – HemChe Dec 05 '12 at 14:07

13 Answers13

256

You need to download the executable driver from: ChromeDriver Download

Then use the following before creating the driver object (already shown in the correct order):

System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
WebDriver driver = new ChromeDriver();

This was extracted from the most useful guide from the ChromeDriver Documentation.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
aimbire
  • 3,697
  • 1
  • 15
  • 28
  • @aimbire: hey I am facing same problem. I did the same thing suggested by you but getting this error. "java.lang.IllegalStateException: The driver executable does not exist: D:\selenimPRJarg1\chromedriver.exe" Am I missing something"? – kTiwari Jul 13 '14 at 05:10
  • You have to download Selenium Standalone Server from here http://docs.seleniumhq.org/download/, and add the jar file as a dependency to your Java project. – dikirill Oct 28 '15 at 13:13
  • 5
    where do you use/type this? System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver"); WebDriver driver = new ChromeDriver(); – tijnn Nov 29 '15 at 15:57
  • While this is technically correct, you really shouldn't be setting that environmental variable in code, it's the same as hard coding a path in your code. The environmental variable should be set on the machine that you run the code on, allowing each machine to store the chrome driver executable in a unique place but have everything still work. – Ardesco May 02 '19 at 09:55
21

Download the updated version of the Google Chrome driver from Chrome Driver.

Please read the release note as well here.

If the Chrome browser is updated, then you need to download the new Chrome driver from the above link, because it would be compatible with the new browser version.

public class chrome
{

    public static void main(String[] args) {

        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
        WebDriver driver = new ChromeDriver();

        driver.get("http://www.google.com");
    }

}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ankit jain
  • 4,198
  • 4
  • 19
  • 24
18

You should download the chromeDriver in a folder, and add this folder in your PATH environment variable.

You'll have to restart your console to make it work.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Fabrice31
  • 770
  • 11
  • 25
8

If you're using Homebrew on a macOS machine, you can use the command:

brew tap homebrew/cask && brew cask install chromedriver

It should work fine after that with no other configuration.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
swanhella
  • 441
  • 6
  • 14
6

You need to install the Chrome driver. You can install this package using NuGet as shown below:

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Prathap Kudupu
  • 1,315
  • 11
  • 11
6

You can use the below code to run test cases in Chrome using Selenium WebDriver:

import java.io.IOException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class ChromeTest {

    /**
     * @param args
     * @throws InterruptedException
     * @throws IOException
     */
    public static void main(String[] args) throws InterruptedException, IOException {
        // Telling the system where to find the Chrome driver
        System.setProperty(
                "webdriver.chrome.driver",
                "E:/chromedriver_win32/chromedriver.exe");

        WebDriver webDriver = new ChromeDriver();

        // Open google.com
        webDriver.navigate().to("http://www.google.com");

        String html = webDriver.getPageSource();

        // Printing result here.
        System.out.println(html);

        webDriver.close();
        webDriver.quit();
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rakesh Chaudhari
  • 3,310
  • 1
  • 27
  • 25
4

Find the latest version of chromedriver here. Once downloaded, unzip it at the root of your Python installation, e.g., C:/Program Files/Python-3.5, and that's it.

You don't even need to specify the path anywhere and/or add chromedriver to your path or the like. I just did it on a clean Python installation and that works.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
tagoma
  • 3,896
  • 4
  • 38
  • 57
4

Download the latest version of the Chrome driver and use this code:

System.setProperty("webdriver.chrome.driver", "path of chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
Thread.sleep(10000);
driver.get("http://stackoverflow.com");
    
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Anuj Teotia
  • 1,303
  • 1
  • 15
  • 21
1

On Ubuntu, you can simply install the chromium-chromedriver package:

apt install chromium-chromedriver

Be aware that this also installs an outdated Selenium version. To install the latest Selenium:

pip install selenium
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
serv-inc
  • 35,772
  • 9
  • 166
  • 188
0

All the previous answers are correct. Following is the little deep dive into the problem and solution.

The driver constructor in Selenium for example

WebDriver driver = new ChromeDriver();

searches for the driver executable, in this case the Google Chrome driver searches for a Chrome driver executable. In case the service is unable to find the executable, the exception is thrown.

This is where the exception comes from (note the check state method)

 /**
   *
   * @param exeName Name of the executable file to look for in PATH
   * @param exeProperty Name of a system property that specifies the path to the executable file
   * @param exeDocs The link to the driver documentation page
   * @param exeDownload The link to the driver download page
   *
   * @return The driver executable as a {@link File} object
   * @throws IllegalStateException If the executable not found or cannot be executed
   */
  protected static File findExecutable(
      String exeName,
      String exeProperty,
      String exeDocs,
      String exeDownload) {
    String defaultPath = new ExecutableFinder().find(exeName);
    String exePath = System.getProperty(exeProperty, defaultPath);
    checkState(exePath != null,
        "The path to the driver executable must be set by the %s system property;"
            + " for more information, see %s. "
            + "The latest version can be downloaded from %s",
            exeProperty, exeDocs, exeDownload);

    File exe = new File(exePath);
    checkExecutable(exe);
    return exe;
  }

The following is the check state method which throws the exception:

  /**
   * Ensures the truth of an expression involving the state of the calling instance, but not
   * involving any parameters to the calling method.
   *
   * <p>See {@link #checkState(boolean, String, Object...)} for details.
   */
  public static void checkState(
      boolean b,
      @Nullable String errorMessageTemplate,
      @Nullable Object p1,
      @Nullable Object p2,
      @Nullable Object p3) {
    if (!b) {
      throw new IllegalStateException(format(errorMessageTemplate, p1, p2, p3));
    }
  }

SOLUTION: set the system property before creating driver object as follows.

System.setProperty("webdriver.gecko.driver", "path/to/chromedriver.exe");
WebDriver driver = new ChromeDriver();

The following is the code snippet (for Chrome and Firefox) where the driver service searches for the driver executable:

Chrome:

   @Override
    protected File findDefaultExecutable() {
      return findExecutable("chromedriver", CHROME_DRIVER_EXE_PROPERTY,
          "https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver",
          "http://chromedriver.storage.googleapis.com/index.html");
    }

Firefox:

@Override
 protected File findDefaultExecutable() {
      return findExecutable(
        "geckodriver", GECKO_DRIVER_EXE_PROPERTY,
        "https://github.com/mozilla/geckodriver",
        "https://github.com/mozilla/geckodriver/releases");
    }

where CHROME_DRIVER_EXE_PROPERTY = "webdriver.chrome.driver" and GECKO_DRIVER_EXE_PROPERTY = "webdriver.gecko.driver"

Similar is the case for other browsers, and the following is the snapshot of the list of the available browser implementation:

Enter image description here

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
chuha.billi
  • 159
  • 1
  • 6
0

To run Selenium WebDriver test cases in Chrome, follow these steps:

  1. First of all, set the property and Chrome driver path:

    System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
    
  2. Initialize the Chrome Driver's object:

    WebDriver driver = new ChromeDriver();
    
  3. Pass the URL into the get method of WebDriver:

    driver.get("http://www.google.com");
    
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ghanshyam
  • 184
  • 8
0

I included the binary into my projects resources directory like so:

src\main\resources\chrome\chromedriver_win32.zip
src\main\resources\chrome\chromedriver_mac64.zip
src\main\resources\chrome\chromedriver_linux64.zip

Code:

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.SystemUtils;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import java.io.*;
import java.nio.file.Files;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public WebDriver getWebDriver() throws IOException {
    File tempDir = Files.createTempDirectory("chromedriver").toFile();
    tempDir.deleteOnExit();
    File chromeDriverExecutable;

    final String zipResource;
    if (SystemUtils.IS_OS_WINDOWS) {
        zipResource = "chromedriver_win32.zip";
    } else if (SystemUtils.IS_OS_LINUX) {
        zipResource = "chromedriver_linux64.zip";
    } else if (SystemUtils.IS_OS_MAC) {
        zipResource = "chrome/chromedriver_mac64.zip";
    } else {
        throw new RuntimeException("Unsuppoerted OS");
    }

    try (InputStream is = getClass().getResourceAsStream("/chrome/" + zipResource)) {
        try (ZipInputStream zis = new ZipInputStream(is)) {
            ZipEntry entry;
            entry = zis.getNextEntry();
            chromeDriverExecutable = new File(tempDir, entry.getName());
            chromeDriverExecutable.deleteOnExit();
            try (OutputStream out = new FileOutputStream(chromeDriverExecutable)) {
                IOUtils.copy(zis, out);
            }
        }
    }

    System.setProperty("webdriver.chrome.driver", chromeDriverExecutable.getAbsolutePath());
    return new ChromeDriver();
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jonas_Hess
  • 1,874
  • 1
  • 22
  • 32
-2

Download the EXE file of chromedriver and extract it in the current project location.

Here is the link, where we can download the latest version of chromedriver:

https://sites.google.com/a/chromium.org/chromedriver/

Here is the simple code for the launch browser and navigate to a URL.

System.setProperty("webdriver.chrome.driver", "path of chromedriver.exe");

WebDriver driver = new ChromeDriver();

driver.get("https://any_url.com");
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ER.swatantra
  • 125
  • 1
  • 5
  • Download the chromedriver and extract it. next create a folder in your project and name it as chromedriver and keep the .exe file in it. and use this path. This is the simple code example. 'code' System.setProperty("webdriver.chrome.driver",System.getProperty("user.dir")+"\\chromedriver\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("http://www.google.co.in"); WebElement searchbox = driver.findElement(By.name("q")); searchbox.sendKeys("best selenium video tutorials free"); searchbox.submit(); – vinay Aug 21 '15 at 05:51
  • 1
    Please, considere updating this answer with the additional info vinay commented. – Ricardo Souza Sep 15 '17 at 16:18