An Introduction to Selenium WebDriver

Selenium has been in the market for over a decade and has proven to be a robust tool for automation testing. Developed by Jason Huggins, its main objective was to overcome the limitations of manual testing. 

What is Selenium?

Selenium is an open-source, automated testing tool used to test web applications across multiple browsers. It's primarily built in Java and supports several browsers and programming languages. 

The Selenium test suite comprises of four tools:

  1. Selenium Integrated Development Environment (IDE)
  2. Selenium Remote Control (RC)
  3. Selenium WebDriver 
  4. Selenium Grid

The article covers Selenium WebDriver in detail, including its features, architecture, the working principles, followed by a demo.

Learn From The Best Mentors in the Industry!

Automation Testing Masters ProgramExplore Program
Learn From The Best Mentors in the Industry!

Need for Selenium WebDriver

To understand why WebDriver was introduced, let's look at the shortcomings of Selenium RC. 

Selenium Remote Control (RC) is a test tool that allows you to write automated web application UI tests in any programming language against any HTTP website using any mainstream JavaScript-enabled browser.

Selenium RC

Fig: Selenium RC

Selenium RC Server receives Selenium commands from your test program, interprets them, and reports the results back to the program. The Web browser is injected with Selenium core, which interprets and executes the Selenese commands used in the test script. The web browser now interacts with the web server accordingly. This setup, however, complicates the architecture and takes additional time for execution. 

Let's see how Selenium WebDriver helped overcome these drawbacks. The WebDriver does not use an additional server, instead of making direct calls to the browser using each browser's native support for automation.

Selenium Webdriver

Fig: Selenium WebDriver

The execution time for commands is shorter due to the simple architecture. WebDriver also provides easy-to-use APIs, unlike RC, which used redundant and confusing APIs. WebDriver also supports a headless, GUI-less HtmlUnit browser. 

What is Selenium WebDriver?

Paul Hammant developed Selenium WebDriver in 2006. Selenium WebDriver was the first cross-platform testing framework that could configure and control the browsers on the OS level. It served as a programming interface to create and run test cases. 

WebDriver performs actions on web elements. It supports various programming languages like Java, C#, PHP, Python, among others. It can also be integrated with frameworks like TestNG and JUnit for test management. 

The architecture of Selenium WebDriver is simple and easy to understand.

Selenium WebDriver Architecture

    Fig: Selenium WebDriver Architecture

  • Selenium test script - Selenium test script is the test code written in any of the mentioned programming languages that are interpreted by the driver 
  • JSON Wire Protocol - JSON Wire Protocol provides a transport mechanism to transfer data between a server and a client. JSON Wire Protocol is the industry standard for various web services
  • Browser drivers - Selenium uses drivers, specific to each browser to establish a secure connection with the browser
  • Browsers - Selenium WebDriver supports multiple web browsers to test and run applications on. 

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Selenium WebDriver Demo

1. Download and Install Java 8 or higher version; Install the latest version Java development kit. Click here to install it.

2. Download and configure Eclipse or any Java IDE of your choice; Open the URL link.

Selenium WebDriver Demo

Scroll down the page and click on the latest version in the "more downloads" section.

The downloaded file will be a zip file. Unzip the file in a folder of your choice. Once unzipped, open the .exe eclipse file.

Selenium WebDriver Demo 1


The next step is to configure a workspace. Select a directory you want to store all your projects in and click on the launch icon.

Selenium WebDriver Demo 2



Once launched, the IDE workbench will look as shown:

Selenium WebDriver Demo3

Download Selenium WebDriver Java Client 

  • Navigate to the official Selenium page 
  • Scroll down through the web page and locate Selenium Client & WebDriver Language Bindings
  • Click on the "Download" link of Java Client Driver as shown in the image below

Selenium WebDriver Demo  4

Once downloaded, unzip the file in a directory. It consists of the jar files required to configure Selenium WebDriver in the IDE.

Selenium WebDriver Demo 5

4. Download the browser driver

The automation scripts must be compatible with any browser. Every browser supported by Selenium comes with its driver files, which are essential to run the scripts. Next, download the latest driver file from this link.

Selenium WebDriver Demo 6

5. Configure Selenium WebDriver 

Now, the final step is to configure the Selenium WebDriver with the Eclipse IDE. In simple terms, we create a new Java project to build our test script.

Selenium WebDriver Demo 7

Provide a project name and select the JRE that you wish to use. It's advisable to use the default JRE. Select it and click on finish.

Selenium WebDriver Demo 8

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

The next and most crucial step is to add the downloaded Java executable files [Step 3]. To do this, Right-click on the project>>Build Path>>Configure Build Path

Selenium WebDriver Demo 9

Select libraries and then Add External JARs.

Selenium WebDriver Demo 10

Open the folders in which you've saved your JAR files and select the two executable JAR files. Click on open to add them.

Selenium WebDriver Demo 11

Click on the libs folder>> Select the files>>Open

Selenium WebDriver Demo 12

Once you're done adding the library files, click on Apply and Close.

Selenium WebDriver Demo 13

With this, you've successfully configured the Webdriver with the Eclipse IDE. You can now go ahead and build your first test script. 

Right-click on Src folder>>New>>Class

Selenium WebDriver Demo 14

Once you’ve created a class, copy the following code to launch the browser:

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

    public class FirstSeleniumTest {

    WebDriver driver; 

    public void launchBrowser() {

        System.setProperty("webdriver.gecko.driver","C:\\Users\\chinmayee.deshpande\\Downloads\\Drivers\\geckodriver.exe");

        driver = new FirefoxDriver();

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

    }

public static void main(String[] args) throws InterruptedException {

        FirstSeleniumTest obj = new FirstSeleniumTest(); 

        obj.launchBrowser(); 

       }

Note: The second argument to the setProperty method is the location of your browser driver. In our case, we've installed the gecko driver. Hence, we paste the path along with the name of the file. 

Now that we're done with the installation and setup, we can look into some of the commonly used browser APIs.

Browser API

Fig: Browser APIs

Let's now create a simple test script that does the following.

Use case 1: As a user, I want to login to eBay.in and search for "JBL Speakers." Then I want to check the day's deals.

Use case 2: From eBay.in, navigate to the Simplilearn website. And navigate back to eBay.in

Use case 3: From eBay.in, I want to print the page title and close the browser

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Use Case 1 

We create a new method for this use case. Let's call it, searchProduct. 

The first thing to do is navigate to www.ebay.com.

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

Once you navigate to the page, you need to type "JBL Speakers" in the search box. For that, you need first to locate the search box, and you can do so by using the locator APIs. 

Right-click anywhere on the page>>Inspect

1. Now click on the top-left icon. This icon provides the location for the part of the UI selected.

Selenium Use Case

2. Hover the mouse over the search box and click on it. You can now see the HTML code corresponding to that location.

Selenium Use Case 1

3. Copy that code. In this case, we're locating the element using the CSS selector.

Right-click on the code>>Copy>>Copy selector

This code can be pasted in your code.

Selenium Use Case 2

Follow the same steps for clicking on the "search" icon and the "daily deals" icon.

Selenium Use Case 3

Selenium Use Case 4

So the code will look something like this:

driver.findElement(By.cssSelector("#gh-ac")).sendKeys("JBL Speakers");

driver.findElement(By.cssSelector("#gh-btn")).click();

driver.findElement(By.cssSelector("#gh-p-1 > a")).click(); 

The following is the test script for the first use case:

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

    public class FirstSeleniumTest {

    WebDriver driver; 

    public void launchBrowser() {

               System.setProperty("webdriver.gecko.driver","C:\\Users\\chinmayee.deshpande\\Downloads\\Drivers\\geckodriver.exe");

        driver = new FirefoxDriver();

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

    }

    public void searchProduct() throws InterruptedException {

        Thread.sleep(2000);

        driver.findElement(By.cssSelector("#gh-ac")).sendKeys("JBL Speakers");

        driver.findElement(By.cssSelector("#gh-btn")).click();

        Thread.sleep(2000);

        driver.findElement(By.cssSelector("#gh-p-1 > a")).click(); 

    }

    public static void main(String[] args) throws InterruptedException {

        FirstSeleniumTest obj = new FirstSeleniumTest(); 

        obj.launchBrowser(); 

        obj.searchProduct(); }

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Use Case 2

From the eBay homepage, we next navigate to the Simplilearn website. For that, we create a new method, "navigate," and use the navigation commands. 

The following code will help navigate to the Simplilearn and back to ebay.com:

public void navigate() throws InterruptedException {

        Thread.sleep(2000);

        driver.navigate().to("https://www.simplilearn.com/");

        Thread.sleep(2000);

        driver.navigate().back();

    }

Use Case 3 

To print the title of the page, we use the SysOut command. In the class "searchProduct" type the following command: 

System.out.println("The title of the page is :" + driver.getTitle()); 

It's always good practice to create a separate method to close the browser, so create a method called "closeBrowser." 

public void closeBrowser() {

        driver.quit();

    }

So the final code would look something like this: 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

    public class FirstSeleniumTest {

     WebDriver driver; 

     public void launchBrowser() {

       System.setProperty("webdriver.gecko.driver","C:\\Users\\chinmayee.deshpande\\Downloads\\Drivers\\geckodriver.exe");

        driver = new FirefoxDriver();

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

    }

    public void searchProduct() throws InterruptedException {

        Thread.sleep(2000);

        driver.findElement(By.cssSelector("#gh-ac")).sendKeys("JBL Speakers");

        driver.findElement(By.cssSelector("#gh-btn")).click();

        Thread.sleep(2000);

        driver.findElement(By.cssSelector("#gh-p-1 > a")).click();

        System.out.println("The title of the page is :" + driver.getTitle());   

    }

    public void navigate() throws InterruptedException {

        Thread.sleep(2000);

        driver.navigate().to("https://www.simplilearn.com/");

        Thread.sleep(2000);

        driver.navigate().back();

    }

    public void closeBrowser() {

        driver.quit();

    }

    public static void main(String[] args) throws InterruptedException {

        FirstSeleniumTest obj = new FirstSeleniumTest(); 

        obj.launchBrowser(); 

        obj.searchProduct();

        obj.navigate();

        obj.closeBrowser();

    }

Now go ahead and run the code. Run>>Run As>>Java Application

Once the test script runs successfully, you can verify if the title of the page was printed. Scroll down the console to see the message.

Selenium Use Case 5

With that, you've created your first Selenium test case!

Want More Hands-On Training?

To learn more about Selenium and how the various suite components work, check out the Simplilearn's video on "Selenium Tutorial for Beginners" curated by industry experts. In the video, you'll learn about the tool, and have a hands-on demo on the working of Selenium IDE and WebDriver.

If you’re interested in making a career as an automation engineer, the Automation Testing Masters will definitely come in handy and will help you master the complete Selenium suite.

The certification course is ideal for:

  • Test Managers 
  • Test Engineers
  • Test Leads
  • Test Analysts
  • QA Engineers
  • Software Developers
  • Engineers who want to learn automation testing

About the Author

Chinmayee DeshpandeChinmayee Deshpande

Chinmayee is a Research Analyst and a passionate writer. Being a technology enthusiast, her thorough knowledge about the subject helps her develop structured content and deliver accordingly.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.