How To Launch Chrome Browser In Selenium WebDriver?
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.
Hi
I am able to launch the chorme browser but test scripts are not running. I have also tried with your above code and get the same results. Please reply.
This would be happening due to updated version of ur Chrome Browser or you might be using some ols Selenium standalone jar.. Download the recent jar and place in your build path
Hello sir,
please send me all things needed for beginner i have just started my carrier as tester
in given mail id :-bibhishandhagate@gmail.com
thank sir
Thank you for your visit and will help you everyway
Hi sir for Ubuntu what should i do
System.setProperty(“webdriver.chrome.driver”, “E:\DD MISHRA\workspace\chromedriver_win_26.0.1383.0\chromedriver.exe”);
the above line gives error because in ‘\’ is followed by b,t or n,so to change the meaning of ‘\’ replace it by \\.
so change it
System.setProperty(“webdriver.chrome.driver”, “E:\DD MISHRA\\workspace\\chromedriver_win_26.0.1383.0\\chromedriver.exe”);
Hello Dwarika Dhish Mishra ,
I tried to run the simple program which you have provided above, It is showing below error after running the code.
Could you please help me out to resolved this problem
org.openqa.selenium.WebDriverException: Chrome did not respond to ‘GetChromeDriverAutomationVersion’. Elapsed time was 21 ms. (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 11.58 seconds
Build info: version: ‘2.39.0’, revision: ‘ff23eac’, time: ‘2013-12-16 16:12:12’
System info: host: ‘D3SSPYX1’, ip: ‘192.168.156.20’, os.name: ‘Windows 7’, os.arch: ‘x86’, os.version: ‘6.1’, java.version: ‘1.7.0_10’
Driver info: org.openqa.selenium.chrome.ChromeDriver
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:193)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:216)
at org.openqa.selenium.chrome.ChromeDriver.startSession(ChromeDriver.java:182)
at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:111)
at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:115)
at org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:161)
at org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:150)
at org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:107)
at myPckg.Chrome.launchChrome(Chrome.java:15)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
java.lang.NullPointerException
at myPckg.Chrome.kill(Chrome.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:33)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Its done I have updated the version of chromedriver exe.
Hi,
I must say you doing wonderful job helping others by providing suitable answer with quick response hats off to you!!
please send me all things needed for beginner i have just started learning automation
in given mail id :-apar_11@yahoo.com
Hi guys,
here is my code :
“import java.sql.Driver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class TestAutomation {
public static void main(String[] args) {
String path = ” E:/JARs/chromedriver_win32/chromedriver.exe”;
System.setProperty(“webdriver.chrome.driver”,path + ” Launching Chrome”);
WebDriver d = new ChromeDriver();
d.manage().window().maximize();
}
}
”
I get error like :
“Exception in thread “main” java.lang.IllegalStateException: The driver executable does not exist: E:\QA\EQA\ E:\JARs\chromedriver_win32\chromedriver.exeLaunching Chrome
at com.google.common.base.Preconditions.checkState(Preconditions.java:199)
at org.openqa.selenium.remote.service.DriverService.checkExecutable(DriverService.java:121)
at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:116)
at org.openqa.selenium.chrome.ChromeDriverService.access$0(ChromeDriverService.java:1)
at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:137)
at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:296)
at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:88)
at org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:116)
at TestAutomation.main(TestAutomation.java:18)
”
Can u provide me solution ,
I would suggest to just copy the chromedriver.exe in your project and in place of path dorectly write it like this
SYSTEM.setProperty(“webdriver.chrome.driver”, “chromedriver.exe”)
And don’t place any thing after chrimedriver.exe like launching chrome
use double slash on giving path to chromebrower . for eg use \\ instead of \
E:\\QA\EQA\\ E:\\JARs\\chromedriver_win32\\chromedriver.exe
this would be the correct path. Give it a try 🙂
hai sir,
greetins for the day…….
recently i faced one question called
how do you launch browsers without using system.setpropety(“”,””);
please let me out from this
There two more way .. Add it in build path the way you add jar files, and second way is to add it in path the way you add java classpath as system variable.
Hi Sir,
I am using latest chrome Driver 2.21 and latest selenium server that is 2.53 but while running the selenium script with chrome browser ,the console message is “Starting ChromeDriver 2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4) on port 64445
Only local connections are allowed.”
The browser opens but nothing is displaying there..its just round in round and nothing opens.
Check it with selenium standalone jar of version 2.52 might be something wrong would be with current jar
I have also tried earlier with 2.48 but failed similarly and 2.53 is giving same issue.It doesn’t seems that its an issue with the selenium standalone server
Hi,
Issue with Chrome Browser communication with selenium script
I am using the code mentioned below,I am using selenium server 2.53 and chrome driver 2.21 ,both are latest according to selenium.org.
The issue is Chrome browser opens but nothing displays it keeps on running,Just the message in console is
“Starting ChromeDriver 2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4) on port 54702
Only local connections are allowed”.
public class first {
public static void main(String[] args) {
{
System.setProperty(“webdriver.chrome.driver”,”D:\\chromedriver_win32\\chromedriver.exe”);
WebDriver driver = new ChromeDriver();
driver.navigate().to(“http://www.msn.com”);
}
}
}
I was previously using 2.48 selenium server for chrome browser but still the issue was same.
Have you tried some other site or not what i can see that your code is able to open the browser but it is not able to open the site
Hi,
This is the code which I am using,i have tried opening facebook,google ,msn but none of the site opens .Its just the browser opens and nothing displays in it.
public class first {
public static void main(String[] args) {
{
System.setProperty(“webdriver.chrome.driver”,”D:\\chromedriver_win32\\chromedriver.exe”);
WebDriver driver = new ChromeDriver();
driver.navigate().to(“http://www.google.com”);
}
}
}
I would suggest to download both stuff again and try again but before that watch the video attached with this post.
Thanks for the Reply !
I was previously using selenium 2.48.2 and was facing the same issue with Chrome Browser and now when I have updated it to 2.53 still the issue is same.
I am trying Page Object Model without using PageFactory
LoginPage:
/**
*
*/
package com.wordpress.Pages;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
/**
* @author waheed.ahmed
*
* This class will store all locators and methods of Login page
*
*/
public class LoginPage {
WebDriver driver;
//a. Username or Email Address field
By username = By.id(“user_login”);
//b. Password field
By password = By.xpath(“.//*[@id=’user_pass’]”);
//c. Login button
By loginButton= By.name(“wp-submit”);
//constructor
public LoginPage(WebDriver driver)
{
this.driver=driver;
}
//d. Method to type username
public void typeUserName()
{
driver.findElement(username).sendKeys(“admin”);
}
//e. Method to type password
public void typePassword()
{
driver.findElement(password).sendKeys(“demo123”);
}
//f. Method to click on Login Button
public void clickOnLoginButton()
{
driver.findElement(loginButton).click();
}
}
VerifyWordpress:
/**
*
*/
package com.wordpress.Testcases;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import com.wordpress.Pages.LoginPage;
import java.lang.*;
/**
* @author waheed.ahmed
*
*/
public class VerifyWordpressLogin{
WebDriver driver;
@BeforeTest
public void verifyValidLogin()
{
//a.First create ChromeDriver to drive the browser
System.setProperty(“webdriver.chrome.driver”, “C:\\Users\\waheed.ahmed\\Downloads\\eclipse-jee-mars-2-win32-x86_64\\chromedriver_win32\\chromedriver.exe”);
driver = new ChromeDriver();
}
@Test
public void main1() {
//b.Launch Indeed home page.
driver.get(“https://login.wordpress.org/”);
//c. Access the Locator class and pass the driver to initialize
LoginPage login = new LoginPage(driver);
//d. Perform activities
login.typeUserName();
login.typePassword();
login.clickOnLoginButton();
//e.close the browser
driver.quit();
}
}
Output in console:
[TestNG] Running:
C:\Users\waheed.ahmed\AppData\Local\Temp\testng-eclipse-983882900\testng-customsuite.xml
===============================================
Default test
Tests run: 0, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 0, Failures: 0, Skips: 0
===============================================
[TestNG] Time taken by org.testng.reporters.XMLReporter@42d80b78: 5 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 1 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@6a41eaa2: 1 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@39c0f4a: 25 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@3159c4b8: 26 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@5025a98f: 3 ms
It doesnt do anything meaning doesnt launch chrome browser I am using selenium 3 with testng and chrome driver is 2.26/5 i believe I dont even see any errors
Hello ,
Can u please send me all things needed for beginner i have just started my carrier as tester
mail id :-gayatri.moyyi76@gmail.com
thank u
Hi,
I am using chrome driver in the same way and all tests are running successfuly from eclipse.However when I am running testng.xml from cmd, I am getting configuration error.
Running below command in project directory:
java -cp “.\bin;.\lib\*” org.testng.TestNG testng.xml
Error in cmd:
Total tests run: 4, Failures: 0, Skips: 4
Configuration Failures: 1, Skips: 1
This might be the cause
1- might be you would be using beforemethod multiple time, one in base class and another in test class
2- you need to put the chromedriver in path so do one thing just place your chromedriver in lib folder it will pick it from there or hard code the chrome path in your test class
3- this might be happening due to modifier used with your method.
So try these things and one more thing could you please check whether this testng.xml is working from eclipse or not..because if it is working in eclipse and correct class path is set during command line execution then it should work correctly.
Good luck …
Hi,
I am getting below error in Ubuntu :
Exception in thread “main” java.lang.IllegalStateException: The driver executable does not exist: /home/xyz-root/var/tmp/chromedriver………………
OS : UBuntu
java: 1.8.0_151
selenium : 3.0.0
code;
Browser :Chrome
Chrome version : Version 62.0.3202.94 (Official Build) (64-bit)
Code : public static void main(String[] args) {
System.setProperty(“webdriver.chrome.driver”,”/home/f-root/var/tmp/chromedriver”);
WebDriver driver = new ChromeDriver();
driver.get(“https://www.google.co.in/?gfe_rd=cr&dcr=0&ei=0yONWocyq4a2B7v2k4AC”);
System.out.println(“Selenium Webdriver Script in “)
}
do one thing kill all chromedriver instance running on your machine and try once more.
when ever you face such scenario, Please try to kill all instance of chrome and then try or download the recent version of selenium standalone jar and executable file.