Launch Chrome Browser Using WebDriver


Launching Chrome Browser using WebDriver

To launch chrome browser in selenium webdriver, we need to create object of chrome driver class ChromeDriver() and needs to be assigned to instance of WebDriver like this




WebDriver driver = new ChromeDriver();
but this code will fail to launch chrome browser and reason for this failure is exception on 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)

So now what we need to do in this case, don’t worry we have one solution to resolve this issue .This could be done by using chromedriver executable that controls chrome So lets see how to do the set-up in our eclipse ide.

Steps:

1- Download zip file of chromedriver for Windows from here
 2- Unzip downloaded Chromedriver for Windows and find the absolute path of chromedriver.exe(Lets assume chromedriver.exe is present at 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 “\”, So let me explain in java if we want to keep any of the special character as it is, we need to put one slash “\” and when compiler encounters “\” then it understand that we need to keep the character after this “\” and this is called Escape Character. So when ever you are going to use any absolute or relative path then i would suggest always use one extra “\”.
and after this line write your traditional line to launch the browser like this
WebDriver driver =new ChromeDriver();

 

Above code is for windows machine but what if you want to launch chrome on mac machine, in that case read Installing Chromedriver on MAC

So why not we write one script that help us to see the launching of Chrome





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();
}
@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 above code and i am pretty sure you would be able to launch chrome browser without any error.

If you want to learn how to run your code in simulator mode then read following post Mobile emulation in Google Chrome using Selenium WebDriver

and once your read this post then i would suggest you to learn Adding add-on in Firefox and Chrome using WebDriver

34 comments

  • Pingback: Selenium Best Links | Learn Testing

  • 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.

  • Hello sir,
    please send me all things needed for beginner i have just started my carrier as tester
    in given mail id :[email protected]
    thank sir

  • Hi sir for Ubuntu what should i do

  • Pingback: Installing Maven Plugin in Eclipse | Abode QA

  • Pingback: Handling JavaScript Alert in WebDriver | Abode QA

  • 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)

  • 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 :[email protected]

  • Pingback: Fix Java Browser Launch Error 193 Windows XP, Vista, 7, 8 [Solved]

  • Pingback: Maximize Window Using Selenium WebDriver And By Using Abstract Window Toolkit - Abode QA

  • 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

  • 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 [email protected]: 5 ms
    [TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 1 ms
    [TestNG] Time taken by [email protected]: 1 ms
    [TestNG] Time taken by [email protected]: 25 ms
    [TestNG] Time taken by [email protected]: 26 ms
    [TestNG] Time taken by [email protected]: 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 :[email protected]
    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.

  • Pingback: Mobile emulation in Google Chrome using Selenium WebDriver

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.