14

I add the below dependency and code for Opening Chrome,but browser is not opening.

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

My code :-

package example;
import org.openqa.selenium.WebDriver;`
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class DepChrome {

    @Test
    public void testBrowser() {
    WebDriver driver = new ChromeDriver();
    driver.manage().window().maximize();
    }
}
Ishita Shah
  • 3,955
  • 2
  • 27
  • 51
Gobi
  • 283
  • 1
  • 5
  • 15
  • Thanks Jain .Can you please share me the idea how to work with chrome using maven dependency ? – Gobi Mar 08 '16 at 12:20

6 Answers6

29

Add below dependency as below:

        <dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>3.0.0</version>
<!--            <scope>test</scope> -->
        </dependency>

Source: copy new dependencies version from below URL:

https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager

use below code :

WebDriver driver = null;
WebDriverManager.chromedriver().browserVersion("77.0.3865.40").setup();
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized"); 
options.addArguments("enable-automation"); 
options.addArguments("--no-sandbox"); 
options.addArguments("--disable-infobars");
options.addArguments("--disable-dev-shm-usage");
options.addArguments("--disable-browser-side-navigation"); 
options.addArguments("--disable-gpu"); 
driver = new ChromeDriver(options); 
driver.get("https://www.google.com/"); 

Basically below line of code did the trick, below code to download a specific version

WebDriverManager.chromedriver().browserVersion("77.0.3865.40").setup();

Required version you can get from below URL:

https://chromedriver.storage.googleapis.com/index.html

you can also use below code instead of above, if you are looking for latest dependencies present on above chromedriver URL

WebDriverManager.chromedriver().setup();

OR (Old Way)

You need to give path of chrome binary as below:

System.setProperty("webdriver.chrome.driver", "C:\\pathto\\my\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com");

Download the binary of chrome from selenium site as below :- http://chromedriver.storage.googleapis.com/index.html?path=2.21/

Now provide the path of the binary to selenium as :-

System.setProperty("webdriver.chrome.driver", "C:\\pathto\\my\\chromedriver.exe");

There is one more thing to take care. if you are using windows then use backward slash \\ and if you are using mac or linux then use forward slash // for setting up the path.

Hope it will help you :)

Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
  • 5
    You can use https://github.com/Ardesco/selenium-standalone-server-plugin to download the binaries as part of the Maven build. – Gili Aug 10 '16 at 13:46
  • 11
    How is this any different from just downloading the chromedriver manually and changing myself? What is the point of using Maven for this if we still have to set it up manually anyway? – Anton Aug 01 '17 at 13:07
  • As Gili Said you can use Ardesco plug-in and other option. check this out :- https://stackoverflow.com/questions/7450416/selenium-2-chrome-driver – Shubham Jain Aug 01 '17 at 13:17
  • Chrome binary file itself is capable to execute script on Chrome, Without using selenium-chrome dependency we can execute on it. Here question belongs with the use of chrome-driver dependency. So there should not be any external download of ChromeDriver.exe if we are adding this dependency. Your answer may work with different zone, compare to actual concern. – Ishita Shah Nov 02 '18 at 09:53
  • java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; – ennth May 28 '21 at 20:16
  • This only worked for me after I used BREW CASK to install the chromedriver (despite having it listed in my dependencies) – ennth May 28 '21 at 20:38
17

There are two ways.

  1. Easiest way is to download chromedriver from the this location

    Download chrome web driver

    Then create a source folder in your project. (Ex : BrowserDrivers) and add downloaded library into this.

    Then set the chrome driver path in the automation script using setProperty command as follow.

            System.setProperty("webdriver.chrome.driver", "BrowserDrivers/chromedriver.exe"); 
    
  2. But there is another way. This is more suited for maven build. Add following dependencies in to the POM.xml file.

    There are 2 dependencies. One for Chrome Driver. But to use chrome driver dependency you have to add webdrivermanager dependency. It is compulsory dependency for browser driver. So always you have to add both of them. For more details refer this link Github Webdriver manager link

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

    And add chrome driver dependency also.

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

    Then in your automation script use this line instead of System.setProperty command to declare chrome driver.

    ChromeDriverManager.getInstance().setup();
    

    UPDATE: the use of ChromeDriverManager is deprecated, use this instead:

    import static io.github.bonigarcia.wdm.DriverManagerType.CHROME;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import io.github.bonigarcia.wdm.WebDriverManager;
    
    WebDriverManager.getInstance(CHROME).setup();
    
froblesmartin
  • 1,527
  • 18
  • 25
Neyomal
  • 1,589
  • 1
  • 12
  • 14
6

Check below code -

package example;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import io.github.bonigarcia.wdm.WebDriverManager;
public class DepChrome  {
    @Test
    public void testBrowser() {
        WebDriver driver;
        WebDriverManager.chromedriver().setup();
        driver = new ChromeDriver();        
        driver.get("https://google.com");
        String title = driver.getTitle();
        System.out.println(title);      
        driver.quit();      
    }
}
Suresh Bhandari
  • 109
  • 1
  • 3
  • Where are you getting this dependency: io.github.bonigarcia.wdm.WebDriverManager – White_King Jan 19 '19 at 18:18
  • You can get io.github.bonigarcia.wdm.WebDriverManager Dependencies with maven: io.github.bonigarcia webdrivermanager – White_King Jan 19 '19 at 18:47
  • Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; – ennth May 28 '21 at 20:12
2

With the following two maven dependencies, you do not need to set system properties at all, this should work

<!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
        <dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>${webdriver-manager.version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-chrome-driver -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-chrome-driver</artifactId>
            <version>${selenium-chrome-driver}</version>
        </dependency>
WebDriver driver;

@BeforeSuite
public void setUp(){
    WebDriverManager.chromedriver().setup();
    driver = new ChromeDriver();
    driver.get("http://www.ebay.in");
    driver.manage().timeouts().implicitlyWait(5000, TimeUnit.MILLISECONDS);
    }
Jemal Miftah
  • 137
  • 2
  • 4
0

I use this code and solved

WebDriverManager.chromedriver().browserVersion("77.0.3865.40").setup();

Abbas
  • 51
  • 4
-2

In Maven, with the use of ChromeDriver.exe:

import static io.github.bonigarcia.wdm.DriverManagerType.CHROME;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import io.github.bonigarcia.wdm.WebDriverManager;

import org.testng.annotations.Test;

public class MavenTest {

    @Test
    public void TestMaven()
    {
    System.setProperty("webdriver.chrome.driver", "D:\\Sumit_Backup\\Automation\\Workspace\\Maven\\src\\Browser\\chromedriver.exe");
    WebDriver driver= new ChromeDriver();
    driver.get("http://testng.org/doc/maven.html");
    driver.manage().window().maximize();
}
}
Artem
  • 3,304
  • 3
  • 18
  • 41