How to run Selenium WebDriver test cases in Chrome

2019-01-01 12:38发布

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 Link

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

10条回答
美炸的是我
2楼-- · 2019-01-01 13:01

Download update version of chrome driver from Chrome Driver Please Read the Release note as well Here If chrome Browser is updated then you need to downloaded new chormedriver from the above link because it would be compact-able with new browser version.

 public class chrome 
 {

  public static void main(String[] args) {

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


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

  }

 }
查看更多
闭嘴吧你
3楼-- · 2019-01-01 13:01

All the answers above 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 chrome driver searches for 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;
  }

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();

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, following is the snapshot of the list of the available browser implementation

enter image description here

查看更多
素衣白纱
4楼-- · 2019-01-01 13:04

If you're using homebrew on a MacOS, you can use the command:

brew install chromedriver

It should work fine after that with no other configuration.

查看更多
怪性笑人.
5楼-- · 2019-01-01 13:10

You need to download the executable driver from: ChromeDriver Download

Then all you need to do is 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.

查看更多
人气声优
6楼-- · 2019-01-01 13:10

Find the latest version of chromedriver here. Once downloaded, unzip it at the root of your python installation, eg 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.

查看更多
回忆,回不去的记忆
7楼-- · 2019-01-01 13:10

Download the exe of chromedriver and extract it on current project location. Here the link, where we can download the latest version of chromedriver.

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

Here the simple code for the launch browser and navigate to url.

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

WebDriver driver = new ChromeDriver();

driver.get("https://any_url.com");
查看更多
登录 后发表回答