20

So I have read all the docs on adding chromedriver to my path and followed all of them. I am on a Mac with selenium2, maven, eclipse, and all the latest drivers:

Error:
The path to the chromedriver executable must be set by the webdriver.chrome.driver system property;

I put chromedriver in my Applications folder and my path looks like:

echo $PATH  
/Users/tcerrato/selenium/BS_Sel_Project/auto_helper/test_scripts:/usr/local/apache-maven-2.2.1//bin:/Users/oracle/oracle/product/10.2.0/db_1/bin:/opt/local/bin:/opt/local/sbin:/Applications:

What am I missing? I cannot run with chrome driver at all. Any help would be great I'm trying random stuff now.

Here is my pom section on selenium:

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium</artifactId>
    <version>2.0rc2</version>
    <type>pom</type>
</dependency>
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-chrome-driver</artifactId>
    <version>2.5.0</version>
</dependency>
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-firefox-driver</artifactId>
    <version>2.6.0</version>
</dependency>
ducati1212
  • 855
  • 9
  • 18
  • 29

10 Answers10

33

Add WebDriverManager to your project:

<dependency>
   <groupId>io.github.bonigarcia</groupId>
   <artifactId>webdrivermanager</artifactId>
    <version>5.1.0</version>
</dependency>

This library downloads the latest version of the WebDriver binary you need and export the proper Java system variable (webdriver.chrome.driver, webdriver.gecko.driver, webdriver.opera.driver, webdriver.edge.driver, webdriver.ie.driver), simply using one of the following sentences respectively:

WebDriverManager.chromedriver().setup();
WebDriverManager.firefoxdriver().setup();
WebDriverManager.operadriver().setup();
WebDriverManager.edgedriver().setup();
WebDriverManager.iedriver().setup();

More info on https://bonigarcia.dev/webdrivermanager/

Boni García
  • 4,618
  • 5
  • 28
  • 44
  • I tried using your plugin but whilst it does download the appropriate binary it seems that jbehave always tries to use the firefix driver even when using the system property driven class org.jbehave.web.selenium.PropertyWebDriverProvider – berimbolo Jun 09 '16 at 13:59
  • Ok Jbehave requires an additional system property to be set: System.setProperty("browser", "chrome"); It would be nice if your set up allowed you to specify that you want to use Jbehave and then it set the additional property on your behalf but it does work nicely if I set this myself. Thanks – berimbolo Jun 09 '16 at 14:11
  • Will this work for different system OS like Linux,Mac etc – SaiPawan Jul 17 '18 at 07:09
  • Confirmed working with Selenium 3.x.x. Thank you very much! – k_rollo Sep 05 '18 at 08:04
22

I am not sure about Maven but this how I set the property webdriver.chrome.driver

System.setProperty("webdriver.chrome.driver", "C:\\pathto\\my\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com");
nilesh
  • 14,131
  • 7
  • 65
  • 79
  • 1
    that worked. nothing I read about even remotely made it that clear almost to easy :) thanks – ducati1212 Sep 19 '11 at 12:55
  • 2
    How would one do this in python? – Greg Gauthier Mar 08 '12 at 18:14
  • 1
    @GregGauthier According to [chromedriver's "getting started" page] you can just specify the path as (optional) argument when creating the webdriver: `driver = webdriver.Chrome('/path/to/chromedriver')` – zpea Jul 08 '12 at 09:10
  • @nilesh- Is there a way to set the Chromedriver path in System variables and call it? How this can be done – Satish Mar 15 '16 at 07:20
  • @Satish not sure why you want to do that. My answer pretty much does the same thing. – nilesh Mar 19 '16 at 18:47
  • 1
    @nilesh When I define the `ChromeDriver` as dependency. How can I set the property relativ to my maven project instead setting a absolute path – bish Nov 30 '16 at 06:38
10

Setting the webdriver.chrome.driver system property via maven can be done by the following (and tested working):

  1. Add systemPropertyVariables configuration to the maven-surefire-plugin in your pom.xml. This is (typically) because surefire is the caller for tests and where system properties will be set.

    <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.7.1</version>
        <configuration>
            <systemPropertyVariables>
                <webdriver.chrome.driver>${webdriver.chrome}</webdriver.chrome.driver>
            </systemPropertyVariables>
        </configuration>
    </plugin>
    
  2. Now define ${webdriver.chrome} somewhere. A good start is a <properties> section in your pom.xml

    <properties>
        <webdriver.chrome>/home/gede/bin/chromedriver</webdriver.chrome>
    </properties>
    

Potentially this could be done better via the use of <profiles> like in Simon Martinelli's example

jeedo
  • 361
  • 2
  • 5
6

You could have a go at using the driver binary downloader maven plugin to download the driver binaries for you (https://github.com/Ardesco/selenium-standalone-server-plugin):

                <plugin>
                    <groupId>com.lazerycode.selenium</groupId>
                    <artifactId>driver-binary-downloader-maven-plugin</artifactId>
                    <version>1.0.7</version>
                    <configuration>
                        <rootStandaloneServerDirectory>${project.basedir}/src/test/resources/selenium_standalone_binaries</rootStandaloneServerDirectory>
                        <downloadedZipFileDirectory>${project.basedir}/src/test/resources/selenium_standalone_zips</downloadedZipFileDirectory>                            
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>selenium</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

This will download the binaries and set a maven property that you can use in your surefire/failsafe configuration like this:

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-failsafe-plugin</artifactId>
                    <version>2.7.2</version>
                    <configuration>                            
                        <systemProperties>                              
                            <!--Set properties passed in by the driver binary downloader-->
                            <phantomjs.binary.path>${phantomjs.binary.path}</phantomjs.binary.path>
                            <webdriver.chrome.driver>${webdriver.chrome.driver}</webdriver.chrome.driver>
                            <webdriver.ie.driver>${webdriver.ie.driver}</webdriver.ie.driver>
                            <webdriver.opera.driver>${webdriver.opera.driver}</webdriver.opera.driver>
                        </systemProperties>
                        <includes>
                            <include>**/*WebDriver.java</include>
                        </includes>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>integration-test</goal>
                                <goal>verify</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

When you instantiate a new driver object the system property pointing to the driver binary location will now be set and it will just work.

Ardesco
  • 7,281
  • 26
  • 49
2

So in the pom you have to set it like this

                  <dependency>
                  <groupId>org.seleniumhq.selenium</groupId>
                  <artifactId>selenium-chrome-driver</artifactId>
                  <version>2.34.0</version>
                  </dependency>

This is a java code to run the chrome using selenium

        System.setProperty("webdriver.chrome.driver","C:/chromedriver.exe");
        WebDriver myD = new ChromeDriver();

In order for you to run Chrome you need to download the chrome driver from here. https://code.google.com/p/chromedriver/downloads/list

Once you have done that then you have to set it in environment variable. Read this https://code.google.com/p/selenium/wiki/ChromeDriver

Thanks,

       Mediha
Madi
  • 49
  • 1
  • 4
0

Try this:

System.setProperty("webdriver.chrome.driver","/location to/chromedriver folder");
WebDriver driver = new ChromeDriver();
driver.get("your.app");
Walery Strauch
  • 6,792
  • 8
  • 50
  • 57
user1307037
  • 419
  • 3
  • 5
0
System.setproperty("webdriver.chrome.driver","your file path here with chromedriver.exe");
webDriver driver=new chromeDriver();
driver.get("http://google.com");
peterm
  • 91,357
  • 15
  • 148
  • 157
0

It works for me without setting webdriver.chrome.driver property. Just by adding chromedriver to PATH

> echo $PATH
/usr/local/bin:/usr/local/sbin:~/bin:/usr/bin:/bin:/usr/sbin:/sbin
>    
> which chromedriver
/usr/local/bin/chromedriver

If you use Homebrew, installing chromedriver along with adding to PATH can be done as simple as this:

brew install chromedriver

Useful links:

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

http://brewformulas.org/Chromedriver

Andrey
  • 6,526
  • 3
  • 39
  • 58
-1

Just add WebDriverManager in your maven pom and it works without manual setup if you have your browser setup in default config.

Mikey
  • 380
  • 4
  • 15
-1
    Pom.xml code and Selenium code below:


   <groupId>com.HelloWorld</groupId>
   <artifactId>t</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>

   <name>t</name>
   <url>http://maven.apache.org</url>

   <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>




     <webdriver.chrome>/home/gede/bin/chromedriver</webdriver.chrome>

     </properties>

     <build>
     <resources>
        <resource>
            <directory>src/main/java/resources</directory>
            <filtering>true</filtering> 
        </resource>
      </resources>
      <plugins>

      <plugin>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.7.1</version>
       <configuration>
        <systemPropertyVariables>
            <webdriver.chrome.driver>${webdriver.chrome}
       </webdriver.chrome.driver>
        </systemPropertyVariables>
      </configuration>
       </plugin>


      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.20</version>
        <configuration>
          <suiteXmlFiles>
            <suiteXmlFile>testng.xml</suiteXmlFile>
          </suiteXmlFiles>
        </configuration>
        </plugin>

      </plugins>


       </build>
       <dependencies>

       <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
      </dependency>

      <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-
     chrome-driver -->
     <dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-chrome-driver</artifactId>
    <version>3.8.1</version>
    </dependency>



       <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.4.0</version>
      </dependency>  
      <dependency>
     <groupId>org.testng</groupId>
     <artifactId>testng</artifactId>
      <version>6.8</version>
     <scope>test</scope>
     </dependency>

     <dependency>   
            <groupId>org.seleniumhq.selenium</groupId>   
            <artifactId>selenium-chrome-driver</artifactId>   
            <version>3.8.1</version>   
        </dependency>   

        <dependency>
       <groupId>io.github.bonigarcia</groupId>
       <artifactId>webdrivermanager</artifactId>
      <version>2.1.0</version>
      </dependency>



      <dependency>
      <groupId>com.relevantcodes</groupId>
      <artifactId>extentreports</artifactId>
     <version>2.41.2</version>
     </dependency>



      <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-api</artifactId>
      <version>2.8.2</version>
      </dependency>
      <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>2.8.2</version>
      </dependency>
     </dependencies>
</project>


 Selenuim Code 

public class App 
{
static String currentDir = System.getProperty("user.dir");
static WebDriver driver;

   @BeforeClass
    public static void setupClass() {
        ChromeDriverManager.getInstance().setup();
        driver= new ChromeDriver();
        driver.get("https://www.google.com/");
    }


@Test
    public void test() {





    System.out.println( "Hello World!" );

    }
  }