Hello everyone, In this post, we are going to learn how to launch chrome browser in selenium webDriver.
As per the current implementation of Selenium WebDriver, We always need one ChromeDriver.exe (Chromedriver binary) which establish the communication between your selenium script to chrome browser through webdriver JSON wire protocol. Chromedriver also launches one server which helps the browser to execute your selenium script in chrome browser.
To launch chrome browser in selenium webdriver, we need to create the object of chrome driver class ChromeDriver() and needs to be assigned to an instance of WebDriver like this
WebDriver driver = new ChromeDriver();
but this code will fail to launch chrome browser and the reason for this failure is an exception on the IDE console which states similar to this…
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://code.google.com/p/chromedriver/downloads/list
at com.google.common.base.Preconditions.checkState(Preconditions.java:176)
Above Error Stack, Ask us to set the path for chromedriver and this can be done using webdriver.chrome.driver, But now the question arises, from where we could download this chromedriver executable to open chrome browser. But there are many ways to set the path of this chromedriver executable in classpath.
3 Ways to Set ChromeDriver.exe in the classpath.
1- Setting System Property of ChromeDriver.exe in script/chrome.
2- Like we set the classpath of Java or maven, We can directly set the classpath of this chromedriver.exe in our system environment classpath.
3- The third option is to copy the chromedriver.exe in project level in your eclipse selenium project.
Steps to Download chromedriver.exe
1- Open Selenium WebDriver Official Site.
2- As soon as you land on the Selenium Official Site, click on the Download menu and Navigate to Browsers Section and click on the documentation link under Chrome lebal.
3- As soon as, we click on this link, we will land on the chromedriver executable official page. On this page, We need to navigate to the Latest stable release. which looks like this
4- As soon as you click on the Downloads link as shown in the image, It will open chromedriver directory for downloading this executable for Windows, Mac and Linux machines. Since I am working on Windows, So I will click on Windows chromedriver.exe.
But before downloading this chromedriver.exe, We should always check the browser version and this can be done. Navigate to three vertical dots in your chrome browser, which have tooltip saying Customise and Control Chrome Browser, Click on this Go to Help -> about Chrome Browser and See carefully which version is printed on that page and opt for the same version from the list shown below.
5- Now we have downloaded the archived file of Chromedriver.exe. So let’s talk about using this chromedriver to launch the chrome browser in selenium webdriver.
So we are going to list the way of launching chrome through Selenium.
First Way:
2- Unzip downloaded Chromedriver for Windows and find the absolute path of chromedriver.exe(Let’s assume chromedriver.exe is present at the following path “E:\DD MISHRA\workspace\chromedriver_win_26.0.1383.0\chromedriver.exe“).
3- Now set Property of System by using this line
System.setProperty(“webdriver.chorme.driver”,”E:\DD MISHRA\workspace\chromedriver_win_26.0.1383.0\chromedriver.exe”);
Now you would be thinking why I have used “\\” in place of single “\” in the path, So let me explain the concept of escape character in java. If we want to consume a special character as it is in java, we need to put “\” before the special character. When the compiler encounters “\” then it understands that we need to keep the character after this “\” and this is called Escape Character. So whenever you are going to use an absolute or relative path then I would suggest always use a double slash like this “\\”.
and after this line write your traditional line to launch the browser like this
WebDriver driver =new ChromeDriver();
The above code is for windows machine but what if you want to launch chrome browser on a mac machine, in that case, read Installing Chromedriver on MAC
So why not write one script that helps us to see the launching of Chrome(Assuming you have set up the project before reaching this script if not read this past)
Scenario:
1- Launch Chrome Browser Through Selenium Script.
2- Maximize the browser.
3- Open the Google home page in Browser.
4- Enter Selenium String for search.import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Chrome {
WebDriver driver;
@Before
public void launchChrome()
{
System.setProperty("webdriver.chrome.driver", "E:\DD MISHRA\workspace\chromedriver_win_26.0.1383.0\chromedriver.exe");
driver = new ChromeDriver();
//Maximizing Browser
driver.manage().window().maximize();
}
@Test
public void testChrome()
{
driver.get("http://www.google.co.in");
driver.findElement(By.id("gbqfq")).sendKeys("Selenium");
}
@After
public void kill()
{
driver.close();
driver.quit();
}
}
Run the above code and I am pretty sure you would be able to launch the chrome browser using selenium webdriver without any error.
If you want to learn how to run your code in simulator mode then read the following post-Mobile emulation in Google Chrome using Selenium WebDriver
Read Adding add-on in Firefox and Chrome using WebDriver
Second Way:
1- Assuming, you have an absolute path of selenium chromedriver.exe/
2- Extract it and now copy the chromedriver.exe and navigate to your Eclipse Project which you have set up to create your selenium scripts and paste this selenium chromedriver executable file in eclipse project.
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Chrome {
WebDriver driver;
@Before
public void launchChrome()
{
driver = new ChromeDriver();
}
@Test
public void testChrome()
{
driver.get("http://www.google.co.in");
driver.findElement(By.id("gbqfq")).sendKeys("Selenium");
}
@After
public void kill()
{
driver.close();
driver.quit();
}
}
This will work for you. But if you want to set this for all the project in that case. Give the path of your chormedriver in windows machine classpath. the way we set the Java Classpath.
So the third way is like an assignment for you and for reference see how we set the java classpath in windows.
Leave a Reply