Showing posts with label selenium script in chrome browser. Show all posts
Showing posts with label selenium script in chrome browser. Show all posts

Tuesday, October 9, 2018

First WebDriver Script on Chrome browser : Selenium WebDriver tutorial

Hi guys, In this post, I am going to discuss how to execute first selenium script in Chrome browser.


 Pre-requisite - 

  • Make sure you have already installed Eclipse IDE.
  • Make sure you have already configured WebDriver-jar file with your java project.
  • Make sure you have downloaded the latest version of WebDriver-jar file. Otherwise, you will get a compiler error.
  • Make sure you have downloaded the driver for Chrome. I will show you below how to do it. 
Note - If you don't know how to configureWebDriver-jar with your java project, then refer the link Configure WebDriver jar files with Java Project





How to run Selenium WebDriver script in Chrome browser




1. Open Eclipse.

2. Create one Java Project for example SeleniumTutorial.






3.  Right click on SeleniumTutorial and navigate as shown below - 






4. Create one package lets say com.selenium and click on Finish.
5. In the same way, right click on com.selenium and create one class for example SeleniumTutorial.







6.  Finally, your class will be created and will be shown as - 











How to download ChromeDriver?

  • Refer the link - Download Chrome Driver
  • Download the latest version zip file based on 32-bit or 64-bit supported by your PC and also based on your OS. 





  • I am using window 64 bit. Open the downloaded file and extract it. It will look like - 





  • Open the extracted folder and copy the path of the file. We are going to use it in our program.














Now, we will start writing script - 

1. Under the main section, write - 

System.setProperty("webdriver.chrome.driver", "F:\software\chromedriver_win32");

System.setProperty has two parameters -

  • First parameter - We pass the name of the driver.
  • Second parameter - We pass the path of the extracted jar file. 







Note - If you notice you are getting an error. The reason is that Eclipse IDE does not understand single '\'. So, we have to provide '\\' and append the provided path with the file name with .exe extension as shown below -





Note - Now, you won't get any error.

2. Next, Write below statement -

WebDriver driver = new ChromeDriver();

Here, 'driver' is the reference variable of WebDriver Interface. An object of ChromeDriver class has been created. 'driver' reference is referring to the ChromeDriver object. Chromium provides 'ChromeDriver' to control Chrome browser while automating. Since the reference to the object of ChromeDriver is assigned to 'driver', now 'driver' has control over the Chrome browser.



Note - You will get two errors. We have used an Interface and a Class and as discussed above, an Interface and Class implementations reside in a particular package, we need to import those respective packages. If you hover over where the errors are showing, you will be suggested with some actions to perform. what you need to do is to import below two packages -

  • org.openqa.selenium.WebDriver;
  • org.openqa.selenium.chrome.ChromeDriver;

3. We will open the Google homepage.

driver.get("https://www.google.com");

Note:- get() - command is used to open a specific URL in the opened browser.

4. Now, we will close the browser.

 - driver.quit();


Complete Code -

  package com.selenium;   
  import org.openqa.selenium.WebDriver;   
  import org.openqa.selenium.chrome.ChromeDriver;   
  public class SeleniumTutorial {   
   public static void main(String args[])   
   {   
    System.setProperty("webdriver.chrome.driver", "F:\\software\\chromedriver_win32\\chromedriver.exe");   
    WebDriver driver = new ChromeDriver();   
    driver.get("https://www.google.com/");   
    driver.quite();   
   }   
  }   



That all guys!!! 


In my next post, I will discuss how to create selenium script to execute in IE.



Do comment in the comment box and share your views about the post. If you have any query, please comment on it. It will help me to reach to you and help you to resolve your query.