Guide To Selenium WebDriver: Getting Started With Test Automation [Tutorial]
Tags: webdev, programming, javascript, tutorial
Published: 2022-04-13 10:26:40

When it comes to [web automation testing](https://www.lambdatest.com/?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog), there are a number of frameworks like [Selenium](https://www.lambdatest.com/selenium?utm_source=devto&utm_medium=organic&utm_campaign=sep09_rd&utm_term=rd&utm_content=webpage), [Cypress testing](https://www.lambdatest.com/cypress-testing?utm_source=devto&utm_medium=organic&utm_campaign=sep09_rd&utm_term=rd&utm_content=webpage), [Playwright](https://www.lambdatest.com/playwright-testing?utm_source=devto&utm_medium=organic&utm_campaign=sep09_rd&utm_term=rd&utm_content=webpage), Puppeteer, etc., that make it to the ‘preferred list’ of frameworks. The choice of test automation framework depends on a range of parameters like type, complexity, scale, along with the framework expertise available within the team. However, it’s no surprise that Selenium is still the most preferred framework among developers and QAs.

For starters, Selenium WebDriver is the core of the Selenium automation framework. In this Selenium WebDriver tutorial, I deep dive into the integral aspects of Selenium WebDriver along with touching upon the Selenium WebDriver architecture.

By the end of this Selenium WebDriver tutorial, you will be in a better position to run Selenium WebDriver tests on local Selenium Grid and cloud [Selenium](https://www.lambdatest.com/selenium-automation?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog) Grid.

## What is Selenium?

Selenium is one of the most widely used open-source Web UI (User Interface) automation testing frameworks. It enables testers to execute tests across different browsers, platforms, and languages.

During my QA career, I had the fortune to try out and use a number of test automation frameworks. So far, none of the frameworks come close to the Selenium automation framework in terms of framework features, supported languages, CI/CD integrations, and more. Nevertheless, as per a survey, Selenium automation testing is still the most preferred mode for realizing web automation testing.

![](https://cdn-images-1.medium.com/max/3200/1*RC2ReIgaBMO2u-M1EEJQMg.png)

Selenium is one-up concerning other test automation frameworks, as it provides compatibility with popular programming languages like Python, Java, JavaScript, C#, and Ruby. Therefore, referring to individual Selenium language tutorials like the [Selenium Python tutorial](https://www.lambdatest.com/learning-hub/python-tutorial?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog) can be a good starting point to make the most out of Selenium and Python for web automation testing.

Shown below is the evolution of the Selenium automation framework:

![](https://cdn-images-1.medium.com/max/2748/1*2f0xX_6-xWN5lGjHZjZEHQ.png)

Apart from Selenium WebDriver, [Selenium IDE](https://www.lambdatest.com/blog/selenium-ide-what-is-it-why-is-it-must-for-every-qa/?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog) and Selenium Grid are other key components of the Selenium framework. [Selenium 4](https://www.lambdatest.com/learning-hub/selenium-4?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog) is the latest Selenium version at the time of writing this blog. If you are curious about Selenium 4, you can further deep-dive into our blog on [what is new in Selenium 4](https://www.lambdatest.com/blog/what-is-deprecated-in-selenium4/?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog).

{% youtube https://youtu.be/Mmp_FiNIYtQ?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog  %}

Also, you can follow the [LambdaTest YouTube Channel](https://www.youtube.com/channel/UCCymWVaTozpEng_ep0mdUyw?sub_confirmation=1?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog) and stay updated with the latest tutorials around [Selenium](https://www.lambdatest.com/selenium-automation?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog) testing, [Cypress testing](https://www.lambdatest.com/cypress-testing?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog), CI/CD, and more.

## Features of Selenium automation framework

Now that we have covered the basics about what is [Selenium](https://www.lambdatest.com/selenium-automation?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog), lets me walk you through some of the major features of the Selenium automation framework in this section of the Selenium WebDriver tutorial:

* Selenium supports popular programming languages like Java, Python, C#, JavaScript, Ruby, PHP, and Perl; through the use of specific browser drivers.

* Selenium is also a platform-agnostic test automation framework. For example, running [Selenium tests on macOS](https://www.lambdatest.com/blog/selenium-safaridriver-macos/?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog) is very much similar to running Selenium tests on Windows OS.

* Selenium tests can be executed across multiple browsers (e.g., Firefox, Chrome, IE, Safari, etc.) and multiple operating systems (e.g., Windows 10, macOS, etc.). [Running Selenium tests in IE](https://www.lambdatest.com/blog/how-to-run-selenium-tests-using-ie-driver/?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog) is still relevant even in 2021 🙂

* [Parallel testing in Selenium](https://www.lambdatest.com/blog/what-is-parallel-testing-and-why-to-adopt-it/?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog) using the Selenium Grid helps achieve improved browser and test coverage. This eventually helps in expediting the time to market.

* Selenium integration with test automation frameworks supported by Python, Java, JavaScript, etc., helps make the most out of Selenium and the corresponding test automation framework. For example, the [TestNG framework for Selenium](https://www.lambdatest.com/blog/create-testng-project-in-eclipse-run-selenium-test-script/?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog) helps achieve the best out of Selenium and TestNG frameworks.

* Selenium and Continuous Integration lets QA engineers run Selenium automation tests as a part of the CI/CD pipeline. For example, [Selenium Continuous Integration with Jenkins](https://www.lambdatest.com/blog/jenkins-integration-with-selenium-webdriver/?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog) helps run Selenium automation tests as a part of Jenkins pipelines.

* Selenium integration with reporting platforms like Extent, Allure, etc., helps generate and manage test reports. In addition, [reporting tools in Selenium](https://www.lambdatest.com/blog/best-reporting-tools-for-selenium/?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog) provide information about the overall behavior of tests, which helps improve the quality of tests.

* [Running Selenium tests in Docker](https://www.lambdatest.com/blog/run-selenium-tests-in-docker/?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog) containers cuts down the test execution time and minimizes the differences that might arise among different test environments (e.g., staging, production, etc.)

Now that you already know [what is Selenium](https://www.lambdatest.com/selenium?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog), let’s deep dive into various components of Selenium in the next section of this Selenium WebDriver tutorial.

## What are the components of Selenium?

As already stated, Selenium is a framework (i.e., it is not a single software but a suite composed of different components).

Selenium is composed of the following components:

 1. Selenium IDE

 2. Selenium WebDriver

 3. Selenium Grid

It’s important to understand these main components in depth before getting started with a hands-on in this Selenium WebDriver tutorial.

## Selenium IDE

[Selenium IDE](https://www.lambdatest.com/blog/run-selenium-ide-test-on-online-selenium-grid/?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog) is basically a Record/Run tool that is available as a browser plugin for Mozilla Firefox and Google Chrome. You need not have any programming language experience to work with the IDE.

It follows a record-and-playback approach wherein you can record your test steps and export the tests in the preferred Selenium-supported programming language. These exported tests can then be used with Selenium WebDriver.

Selenium IDE in Selenium 4 is more than a playback & recording tool, the features of which can be leveraged to make the most out of Selenium automation testing.

## Selenium WebDriver

Selenium WebDriver is an enhanced version of Selenium RC and overcomes the limitations faced in Selenium RC. It controls the browser by direct communication. It has a faster execution time as compared to IDE and RC.

Selenium RC and WebDriver were merged into one single unit known as Selenium WebDriver 2.0. Over time it has been enhanced for more features and functionalities. In Selenium 3, the JSON Wire protocol was the primary mode of communication between the test code and web browser.

With Selenium 4, JSON Wire protocol is replaced with W3C protocol, which means that encoding and decoding of test requests are no longer required in WebDriver W3C protocol. Therefore, as QA engineers, we can expect less flaky tests with [W3C WebDriver Protocol](https://www.lambdatest.com/blog/selenium4-w3c-webdriver-protocol/#Advantages?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog) in Selenium 4.

## Selenium Grid

Selenium Grid enables the users to perform parallel test execution. It is used along with Selenium RC to run parallel tests across different browsers and machines.

Users can run simultaneous tests in multiple environments, thereby saving a lot of time and expediting the time to market.

Selenium Grid is based on the Hub-Node concept. Until Selenium 3, the Hub and Node jars had to be started separately. With Selenium 4, all the necessary dependencies are available in a single Jar file. Therefore, to use the [Selenium Grid on Selenium 4](https://www.lambdatest.com/blog/selenium-grid-4-tutorial-for-distributed-testing/?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog), you need not start the Hub and Node separately.

That’s not all. Selenium Grid on Selenium 4 supports different operating modes like Standalone Mode, Hub & Node Mode, and Fully-Distributed Mode.

## Selenium WebDriver architecture

In this Selenium WebDriver tutorial, we have covered the basics of Selenium and its components. Now, let’s try to understand Selenium WebDriver in a detailed manner.

As mentioned earlier, Selenium WebDriver is an enhanced version of Selenium RC. It is a web framework that helps us in executing cross browser tests. It also allows us to choose a programming language to create the test scripts.

## Selenium WebDriver architecture in Selenium 3

The Selenium WebDriver architecture in Selenium 3 constitutes of four major components:

* Selenium Client Libraries/ Language Bindings

* JSON Wire Protocol

* Browser Drivers

* Real Browsers

### Selenium Client Libraries

In order to support multiple languages, Selenium developers have built the language bindings. For example, if you are writing your tests in Java, you can use the Java bindings. Client libraries can be downloaded from the official Selenium website.

### JSON Wire Protocol

JSON is an abbreviated form of JavaScript Object Notation. It transfers the data between client and server. It provides support for data structures like objects and arrays, hence doing data reading and writing easier.

It acts as a REST(Representational State Transfer) API to transfer information between the HTTP servers.

![](https://cdn-images-1.medium.com/max/2226/1*LYTdPUW5QI7fStqsHKs8gA.png)

### Browser Drivers

In order to develop a secure connection with the browser, Selenium uses Drivers. Each browser has its own driver that hides the internal logic of the browser’s functionality.

In addition, each automation language has a corresponding browser driver. The following series of actions are performed when a Selenium automation test is triggered:

* Every Selenium command generates a corresponding HTTP request, which is sent to the browser driver.

* This request is routed through the HTTP Server.

* The HTTP Server now drives the instruction execution on the browser.

* The browser sends back the status to the HTTP Server, which forwards it to the automation script.

Some of the browser drivers are ChromeDriver, GeckoDriver, MicrosoftEdge driver, etc.

### Browsers

Browsers are the end-point of our test execution. Selenium supports all major browsers like Firefox, Chrome, Safari, Edge, etc.

## Selenium WebDriver architecture in Selenium 4

One of the major differences between Selenium 3 and Selenium 4 is that Selenium WebDriver in Selenium 4 is W3C Compliant. As seen in the WebDriver architecture in Selenium 3, encoding and decoding of API requests had to be done due to the involvement of the JSON Wire protocol.

![](https://cdn-images-1.medium.com/max/2000/1*LDGwLy5c-zntAT8UGYl4_A.png)

With [W3C WebDriver in Selenium 4](https://www.lambdatest.com/blog/selenium4-w3c-webdriver-protocol/?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog), the tests can directly communicate with the web browser. Since the Selenium WebDriver and web browsers are on the same page (as far as W3C protocol is concerned), you can expect less flakiness and more consistency as far as Selenium automation testing is concerned.

This certification is ideal for testing professionals who want to acquire advanced, hands-on knowledge in Selenium automation testing.

Here’s a short glimpse of the Selenium Advanced certification from LambdaTest:

{% youtube https://youtu.be/DK21aHjuUE4?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog  %}

## Benefits of Selenium WebDriver

Now that we know about the entire architecture of Selenium WebDriver, let’s take a quick look at its advantages in this part of the Selenium WebDriver tutorial.

Here are the major advantages of Selenium WebDriver:

* It supports multiple operating systems like Windows, Mac, Linux, etc.

* It supports multiple programming languages like Java, Python, JavaScript, PHP, C#, etc.

* It is compatible with all modern browsers like Chrome, Firefox, Opera, Safari, and Microsoft Edge. It is also compatible with the near-retiring Internet Explorer (IE) browser.

* It provides faster completion of test scripts as compared to other tools.

## Limitations of Selenium WebDriver

The [advantages of Selenium WebDriver](https://www.lambdatest.com/blog/13-reasons-why-selenium-webdriver-should-be-your-first-choice-for-automation-testing/?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog) outnumber its shortcomings. However, as a QA engineer, it is important to make an informed choice before choosing the best-suited framework for the project.

Here are some of the potential shortcomings of Selenium WebDriver:

* It does not support the automation of Windows-based applications.

* It does not have any in-built reporting mechanism.

* There is no test tool integration for test management in Selenium.

* Out of the box, Selenium WebDriver does not support parallel testing, which makes it very challenging to run larger and complex test suites. If you wish to perform [parallel testing with Selenium](https://www.lambdatest.com/blog/what-is-parallel-testing-and-why-to-adopt-it/?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog), then you need to use the Selenium Grid.

However, you can eliminate the above-mentioned drawbacks by adopting [cloud Selenium Grid](https://www.lambdatest.com/blog/cloud-selenium-testing/?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog) for test automation where Selenium tests can securely run at scale, that too at a rapid pace! [Cross browser testing](https://www.lambdatest.com/feature?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog) platforms like LambdaTest provide a secure, scalable, and reliable [online Selenium Grid](https://www.lambdatest.com/selenium-grid-online?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog) that lets you run Selenium tests in parallel on 2,000+ browser and platform configurations.

In the latter sections of this Selenium WebDriver tutorial, we will deep dive into how to run Selenium tests in parallel on LambdaTest cloud Selenium Grid.

Note: [**Array.prototype.findIndex**](https://www.lambdatest.com/web-technologies/array-find-index?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=web_technologies) - The method of `findIndex()` returns an index of the 1st element in the array which aceepts the testing function provided.

## Selenium WebDriver: Installation and set up

In this section of the Selenium WebDriver tutorial, we look at setting up Selenium WebDriver for the Windows Operating System with Java as the programming language.

For demonstration, I have used the Eclipse IDE. However, you can choose to use the IDE of your choice. Now we are about to get our hands dirty with some code 🙂

Starting with the prerequisites:

 1. Install Java

 2. Install Eclipse IDE

 3. Install Selenium WebDriver

## Install Java

**Step 1**: Go to the official [Java Downloads website](https://www.oracle.com/java/technologies/downloads/?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog) and download the JDK for Windows x64 Installer.

![](https://cdn-images-1.medium.com/max/2592/1*lj5RbZNbsP8qlnBB81Xa0w.png)

**Step 2**: Accept the license agreement in the pop-up next and click on Download.

![](https://cdn-images-1.medium.com/max/2000/1*IeZNHe2U4OhDh-G_QygkDw.png)

**Step 3**: Once the download is done, navigate to the Downloads folder and double click the executable so that Java is installed in your system.

**Set Up Environment Variables**

**Step 1**: Open your PC settings and select ‘Advanced system settings’ from the System dialog window.

![](https://cdn-images-1.medium.com/max/2238/1*08_JfZSUHQWTmCdqYITDMA.png)

**Step 2**: Click on the Environment Variables button on the next window.

![](https://cdn-images-1.medium.com/max/2000/1*pL3FH1eYlIyz-W46f7nIew.png)

**Step 3**: Now, under the System variables, double-click on Path.

![](https://cdn-images-1.medium.com/max/2000/1*K4Nss015Ejo-5oVPbQ5j_w.png)

**Step 4**: Add the complete path of the Java installation directory as per your system location and click OK.

![](https://cdn-images-1.medium.com/max/2000/1*U0HqLXbaFGVP_y_AlEqx4Q.png)

**Step 5**: That’s it. You can now see the topmost path in the screenshot is the Java path in my system.

To verify that the Java is installed and variables are set correctly, open Command Prompt and type: java -version. You will see the Java version installed in your system.

![](https://cdn-images-1.medium.com/max/2000/1*OAzFRjjBHdOCzexFymuiDw.png)

And there you go, your first prerequisite is fulfilled. The next step is installing Eclipse.

## Install Eclipse

**Step 1**: Download Eclipse IDE for Java Developers from the [official Eclipse website](https://www.eclipse.org/downloads/packages/?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog). I would suggest downloading the last stable release. Select the download based on your system architecture. I am using eclipse Mars, but you can choose any of the latest versions.

**Step 2**: Once the download is complete, extract the zip file at a location at your convenience.

![](https://cdn-images-1.medium.com/max/2000/1*oRK7kVErMH0QHaMgRVVHNQ.png)

**Step 3**: Now open the extracted folder and double click on eclipse.exe.

![](https://cdn-images-1.medium.com/max/2000/1*Mk3W08BsGralh9sqhQRvrA.png)

The Eclipse will open up, and there you are done with your second prerequisite of this Selenium WebDriver tutorial.

## Install Selenium WebDriver

The last step in this Selenium WebDriver tutorial is to install the Selenium Standalone Server, which is shown in the steps below:

**Step 1**: Download Selenium Client & WebDriver Language Bindings from the Selenium website. You can download the client libraries corresponding to your language preference. We will be working with Java Client Bindings in this blog.

![](https://cdn-images-1.medium.com/max/2446/1*lJzJjHm_NTDc85DWpzTZrQ.png)

**Step 2**: Once the file is downloaded, extract it to a folder of your choice from where you can use it later.

![](https://cdn-images-1.medium.com/max/2000/1*3w-vWvMBYYuyR3lC-xS41g.png)

**Step 3**: You can now see the corresponding extracted file at the selected destination folder.

![](https://cdn-images-1.medium.com/max/2000/1*Z79MdJyh8U3W4e08ryiwvQ.png)

**Step 4**: On opening the folder, you will be able to see the jar files, a libs folder(which contains some more jar files), and a CHANGELOG file.

![](https://cdn-images-1.medium.com/max/2000/1*GKIeooUKaMHDLe2COzPAxA.png)

**Step 5**: Next, you need to download the browser drivers that will help you instantiate the browser. Below are the links to download the drivers for some major browsers:

* [GeckoDriver(Firefox)](https://github.com/mozilla/geckodriver/releases?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog)

* [InternetExplorerDriver](https://selenium-release.storage.googleapis.com/index.html?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog)

* [ChromeDriver](https://sites.google.com/a/chromium.org/chromedriver/?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog)

There is a [difference between WebDriver and Remote WebDriver](https://www.lambdatest.com/blog/selenium-remotewebdriver/?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog) since there is no necessity to download browser drivers when running Selenium tests on a cloud Selenium Grid.

**Step 6**: Next, simply extract the downloaded driver and store it at a location where you can use it.

![](https://cdn-images-1.medium.com/max/2000/1*14mK4NP7F_NDSl_UaeU4aA.png)

Now we are done with setting up the prerequisites, let’s look at how to configure Eclipse with the Selenium Libraries that we have just installed.

Note: [**Array.prototype.includes**](https://www.lambdatest.com/web-technologies/array-includes?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=web_technologies) - Checks if an array includes the given value, returning a boolean value (unlike indexOf).

## How to setup Eclipse for Selenium WebDriver

The following steps in this Selenium WebDriver tutorial will guide you in configuring and installing the Eclipse IDE:

**Step 1**: Launch eclipse.exe that was saved in the steps to install Eclipse.

**Step 2**: Select your workspace manually, or let the default location be there and click on OK.

![](https://cdn-images-1.medium.com/max/2000/1*2KpRn1geuqYJ8_uy7QML8w.png)

**Step 3**: Create a New Project by navigating to **File > New > Java Project**.

![](https://cdn-images-1.medium.com/max/2000/1*xkdhY_OAm17s-scclQiW1w.png)

![](https://cdn-images-1.medium.com/max/2000/1*qFu-HNAW30BM-UQCqYHO-g.png)

**Step 4**: Click on Next and give a name to the Project. Click on Finish.

![](https://cdn-images-1.medium.com/max/2000/1*X18U91ehMln6xYqIalJMOA.png)

**Step 5**: Now that your project is created, Right-click on the project and select New > Package.

![](https://cdn-images-1.medium.com/max/2000/1*VbkxXzu3oS3QUa2678qr7g.png)

**Step 6**: Enter the package name in the New Java Package dialog box and click on Finish.

![](https://cdn-images-1.medium.com/max/2000/1*HJW1vzfNEFrF_w90Vtcrxw.png)

**Step 7**: You need to create a class under the package now. To do so, right-click on the package name, go to New > Class.

![](https://cdn-images-1.medium.com/max/2000/1*Tatek4BYxczZmJovo8alsw.png)

**Step 8**: In the New Java Class dialog box, enter a name for your class, select a method stub viz, public static void main(String[] args) and click on Finish.

![](https://cdn-images-1.medium.com/max/2000/1*tJmW8kwxaSP5TQtaf6KX-Q.png)

Your Explorer would look like below:

![](https://cdn-images-1.medium.com/max/2000/1*F55c18lUYP8eMklFu9QKDA.png)

**Step 9**: Now that we have created an outline for our test project, we need to import the libraries we installed for the Selenium WebDriver tutorial in the section above.

To start with, right-click on the project and go to Build Path > Configure Build Path.

![](https://cdn-images-1.medium.com/max/2000/1*p5DATk27aE5-IMc0J3-FgA.png)

**Step 10**: Click on Add External JARs and navigate to the location where your downloaded JARs were saved.

![](https://cdn-images-1.medium.com/max/2000/1*K6etv5vP1wCPGHmVWFjEXA.png)

**Step 11**: Select the two jars installed in the Selenium Client folder and the jars under the libs folder.

![](https://cdn-images-1.medium.com/max/2000/1*r_PT5XDM6tgFeLt-nuqhhg.png)

![](https://cdn-images-1.medium.com/max/2000/1*I0IDW0Z3eGjLjNy8lpGuig.png)

**Step 12**: Once added, you will see the jar files under the Libraries:

![](https://cdn-images-1.medium.com/max/2000/1*_v693UhOCB7ERvTyAdLp9A.png)

**Step 13**: Click on Apply and then OK. You can now see the Referenced Libraries populated in the package explorer.

![](https://cdn-images-1.medium.com/max/2000/1*VJqGINCNZmJVt3Akc5LezA.png)

We have now configured Selenium WebDriver in our Eclipse Project and are good to write our first test script.

## Running test automation script using Selenium WebDriver

Now that I have touched upon Selenium Webdriver and its architecture, let’s write our first automation script using Selenium Webdriver in this section of the Selenium WebDriver tutorial.

### Problem Statement

To demonstrate the usage of Selenium WebDriver, I will perform the following use case:

 1. Launch Chrome browser.

 2. Open [LambdaTest sign up page](https://accounts.lambdatest.com/register?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog).

 3. Click on the Sign In button.

 4. Close the web browser.

**Implementation**

    

```
package LambdaTest;
 
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
 
@Listeners({util.Listener.class})
class FirstTestScriptUsingWebDriver {
   public static WebDriver driver = null;
   @BeforeTest
   public void setUp() throws Exception {
       WebDriverManager.chromedriver().setup();
       driver=new ChromeDriver();
   }
   @Test
   public void firstTestCase() {
       try {
           System.out.println("Logging into Lambda Test Sign Up Page");
           driver.get("https://accounts.lambdatest.com/register");
           WebElement pageHeader= driver.findElement(By.xpath("//a[text()='Sign In']"));
           pageHeader.click();
           System.out.println("Clicked on the Sign In Button.");
       } catch (Exception e) {
       }
   }
   @AfterTest
   public void closeBrowser() {
       driver.close();
       System.out.println("The driver has been closed.");
   }
}
```

You will need the below pom.xml for importing the necessary dependencies.

    

```
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
 
  <groupId>org.example</groupId>
  <artifactId>LambdaTest</artifactId>
  <version>1.0-SNAPSHOT</version>
  <dependencies>
      <dependency>
          <groupId>org.seleniumhq.selenium</groupId>
          <artifactId>selenium-api</artifactId>
          <version>4.0.0-alpha-7</version>
      </dependency>
      <dependency>
          <groupId>org.seleniumhq.selenium</groupId>
          <artifactId>selenium-remote-driver</artifactId>
          <version>4.0.0-alpha-7</version>
      </dependency>
      <dependency>
          <groupId>org.seleniumhq.selenium</groupId>
          <artifactId>selenium-chrome-driver</artifactId>
          <version>4.0.0-alpha-7</version>
      </dependency>
      <dependency>
          <groupId>org.testng</groupId>
          <artifactId>testng</artifactId>
          <version>6.14.3</version>
      </dependency>
      <dependency>
          <groupId>io.github.bonigarcia</groupId>
          <artifactId>webdrivermanager</artifactId>
          <version>4.4.3</version>
      </dependency>
  </dependencies>
 
  <properties>
      <maven.compiler.source>8</maven.compiler.source>
      <maven.compiler.target>8</maven.compiler.target>
  </properties>
</project>
```
And the below testng.xml file will be needed for running the test case.

    

```
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite  name="FirstTestScriptUsingWebDriverSuite">
   <test name="FirstTestScriptUsingWebDriver" >
       <classes>
           <class name="LambdaTest.FirstTestScriptUsingWebDriver" >
           </class>
       </classes>
   </test>
</suite>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite  name="FirstTestScriptUsingWebDriverSuite">
   <test name="FirstTestScriptUsingWebDriver" >
       <classes>
           <class name="LambdaTest.FirstTestScriptUsingWebDriver" >
           </class>
       </classes>
   </test>
</suite>
```

**Code Walkthrough**

Import Dependencies: Here, we have imported all the necessary classes for using WebDriver, ChromeDriver, and the related [annotations in TestNG](https://www.lambdatest.com/blog/complete-guide-on-testng-annotations-for-selenium-webdriver/?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog).

![](https://cdn-images-1.medium.com/max/2000/1*RIVYvAF47G53SCBc6Hdo6A.png)

**@BeforeTest**: Typically, for running any Selenium test script, you will need a browser driver, and to use it, you need to set the browser driver executable path explicitly. After that, you need to instantiate the driver instance and proceed with the test case.

![](https://cdn-images-1.medium.com/max/2000/1*MY1Qe8-IoGJ2q7Rkl-AzaA.png)

However, managing the browser driver can become cumbersome over time as their versions keep changing, and you need to keep updating the drivers. Hence, to overcome the same, I have used the WebDriverManagerClass.

![](https://cdn-images-1.medium.com/max/2000/1*UBdmLUVQjQu8fZqtF_E4uw.png)

Here, WebDriverManager.chromedriver.setup() checks for the latest version of the specified web driver binary. If the binaries are not present, it will download them and later instantiate the Selenium WebDriver instance with the ChromeDriver. Isn’t it easy? 🙂

The below two classes will help you in using WebDriver and ChromeDriver.

![](https://cdn-images-1.medium.com/max/2000/1*XYQoxVlakYCcuwzQhdlFeg.png)

**@ Test(firstTestCase)**: In the test case, I opened the [LambdaTest sign up page](https://accounts.lambdatest.com/register?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog) in the Chrome browser using the driver.get() statement. The findElement method in Selenium is used for locating the SignIn WebElement.

Here, I have used [XPath in Selenium](https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog) for locating the ‘Sign In’ button. To investigate the XPath of any element, you can simply right-click on it and click on Inspect. You will be able to spot the locator in the Elements tab. The [click method in Selenium](https://www.lambdatest.com/blog/selenium-click-button-with-examples/?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog) is used for clicking on the Sign In button.

![](https://cdn-images-1.medium.com/max/3200/1*syOIcKtfsM-xb9581dChlQ.png)

**@AfterTest**: In the AfterTest method, close the browser using the driver.close() method.

![](https://cdn-images-1.medium.com/max/2272/1*Ev7f712ujoCy4nbESfRIvA.png)

**Test Execution**

You will see the below output once you run the testng.xml file.

![](https://cdn-images-1.medium.com/max/3200/1*DjUHIZofQ47xnOl7XB6Mzg.png)

## Automated browser testing using cloud Selenium Grid

So far, in this Selenium WebDriver tutorial, I used the local Selenium WebDriver to run a particular test case. Now, as the number of test cases increases, running them parallel with a higher number of threads on a single VM would start giving automation failures.

However, a realistic scenario is where a single test case (or multiple test cases) has to be run across different browser and OS combinations. This is where the [Selenium Grid](https://www.lambdatest.com/blog/why-selenium-grid-is-ideal-for-automated-browser-testing/?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog) comes into the picture. Selenium Grid eases our setup-related woes, along with helping us run tests at scale on a local (or cloud) grid setup.

Selenium Grid makes parallel execution possible by sending commands to remote Web Browser instances from a hub server.

Selenium testing tools like LambdaTest offer a cloud Selenium Grid that lets you run parallel tests across 3000+ different browsers and operating systems. All you need to do is instantiate the remote Selenium WebDriver instead of the local Selenium WebDriver.

With LambdaTest, you don’t have to worry about downloading and configuring hundreds of browsers on your machine as they are readily available on the online Selenium Grid. Instead, all you need to do is [register on LambdaTest](https://accounts.lambdatest.com/register?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog) for free and claim your 100 free automation testing minutes.

Now that I have covered the basics of cloud Selenium Grid, let’s implement the same example using LambdaTest Selenium Grid.

**Problem Statement**

In the below example, I am running the same test case on Chrome (latest) + Windows 10 combination.

 1. Launch Chrome browser on Windows 10

 2. Open [LambdaTest sign up page](https://accounts.lambdatest.com/register?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog).

 3. Click on the Sign In button.

 4. Close the web browser.

**Implementation**

  

```
package LambdaTest;
 
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
 
import java.net.MalformedURLException;
import java.net.URL;
 
@Listeners({util.Listener.class})
class FirstTestScriptUsingSeleniumGrid {
 
  public String username = "YOUR USERNAME";
  public String accesskey = "YOUR ACCESSKEY";
  public static RemoteWebDriver driver = null;
  public String gridURL = "@hub.lambdatest.com/wd/hub";
 
  @BeforeTest
  public void setUp() throws Exception {
      DesiredCapabilities capabilities = new DesiredCapabilities();
      capabilities.setCapability("browserName", "chrome");
      capabilities.setCapability("version", "94.0");
      capabilities.setCapability("platform", "win10"); // If this cap isn't specified, it will just get the any available one
      capabilities.setCapability("build", "FirstTestScript");
      capabilities.setCapability("name", "FirstTestScriptSample");
      try {
          driver = new RemoteWebDriver(new URL("https://" + username + ":" + accesskey + gridURL), capabilities);
      } catch (MalformedURLException e) {
          System.out.println("Invalid grid URL");
      } catch (Exception e) {
          System.out.println(e.getMessage());
      }
  }
 
  @Test
  public void firstTestCase() {
      try {
          System.out.println("Logging into Lambda Test Sign Up Page");
          driver.get("https://accounts.lambdatest.com/register");
          WebElement pageHeader= driver.findElement(By.xpath("//a[text()='Sign In']"));
          pageHeader.click();
          System.out.println("Clicked on the Sign In Button.");
      } catch (Exception e) {
 
      }
   }
 
  @AfterTest
  public void closeBrowser() {
      driver.close();
      System.out.println("The driver has been closed.");
  }
}
```

**Code Walkthrough**

**Global Variables**: As tests have to be run on LambdaTest cloud Selenium Grid, we would be using the below-shown variables.

Here, you can populate the values for your corresponding username and access key, which can be collected by logging into your [LamdaTest Profile Section](https://accounts.lambdatest.com/login?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog). However, the gridURL will remain the same.

![](https://cdn-images-1.medium.com/max/2148/1*rYBeL4XiWawAPHTMI0V85A.png)

**@BeforeTest(Setup Method)**: In Selenium, the RemoteWebDriver class implements the WebDriver interface to execute test scripts through the RemoteWebDriver server on a remote machine.

The below-imported class will help you in using the RemoteWebDriver class.

![](https://cdn-images-1.medium.com/max/2052/1*FYzDDOBuF70dctZpuM-RFw.png)

Here, I have used the LambdaTest [Desired Capabilities Generator](https://www.lambdatest.com/capabilities-generator/?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog) and have set the necessary capabilities of browser name, version, platform, etc.

![](https://cdn-images-1.medium.com/max/3200/1*jBAOAIP6aQvToWdYLwrVMA.png)

The implementation under @ Test and @AfterTest annotations remain unchanged since only the end-point of execution have changed from Selenium Grid’s load to cloud Selenium Grid.

**Execution**

Log on to [LambdaTest Automation Dashboard](https://accounts.lambdatest.com/login?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog) to check the status of the test execution on LambdaTest.

![](https://cdn-images-1.medium.com/max/3200/1*CDk7ASn_BSQ__BZ0cvmx8g.png)

Note: [**asm.js**](https://www.lambdatest.com/web-technologies/asmjs?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=web_technologies) - The asm.js subset of JavaScript provides low-level, portable performance with high-level language semantics. Asm.js is intended to be a target for a compile step from languages like C/C++ into optimized JavaScript.

## What’s new in Selenium WebDriver 4?

So far, in this Selenium WebDriver tutorial, I have covered the essentials of Selenium WebDriver and its architecture in Selenium 3 and Selenium 4. However, as a QA engineer, the innumerable features offered by Selenium 4 fascinate me a lot!
{% twitter https://twitter.com/SeleniumHQ/status/1448289975062220808?ref_src=twsrc%5Etfw%7Ctwcamp%5Etweetembed%7Ctwterm%5E1448289975062220808%7Ctwgr%5E%7Ctwcon%5Es1_&ref_url=https%3A%2F%2Fwww.lambdatest.com%2Fblog%2Fselenium-webdriver-tutorial-with-examples%2F?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog chrome-extension://platform.twitter.com/widgets.js %}
Apart from the renewed Selenium WebDriver architecture, here are some of the other major enhancements in Selenium 4:

## W3C Standardization

A significant change in Selenium 4 is the complete W3C compliance of the WebDriver APIs. This eliminates the need for encoding and decoding the API requests by the JSON wire protocol responsible for communication between browsers and test scripts.

The implementation of this standardization means the WebDriver will now interact directly with the target browser.

## Enhanced Selenium Grid

The earlier versions of Selenium Grid were complex to set up and maintain. However, the new Selenium Grid is easy to set up as there is no need to maintain Hub and Node separately.

The Grid even has an improved GUI, and the support for Docker helps the developers (and testers) to use it on Virtual Machines(VMs). The Grid can also be deployed now on Kubernetes. [Running Selenium tests in Docker](https://www.lambdatest.com/blog/run-selenium-tests-in-docker/?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog) is now a cakewalk with the Selenium Grid setup on Selenium 4!

## Relative Locators in Selenium 4

Relative locators (or Friendly locators) help locate WebElements relative to a particular WebElement. For example, the required element could be near to, left of, right of, above, or below a specified element.

[Relative locators in Selenium 4](https://www.lambdatest.com/blog/selenium-4-relative-locator/?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog) can help save a lot of time when locating tricky WebElements. Above, Below, To Right Of, To Left Of, and Near are some of the relative locators that can be leveraged to reduce the lines of code when it comes to locating WebElements.

## Better Browser and Tab Management

Selenium 4 provides a new API newWindow that lets you create a new window (or tab) and switch to it without creating a new WebDriver object.

**Opening a new Window in Selenium 4**

    

```
driver.get("https://www.google.com/");
// Opens a new window and switches to new window
driver.switchTo().newWindow(WindowType.WINDOW);
// Opens LambdaTest homepage in the newly opened window
driver.navigate().to("https://www.lambdatest.com/");
```

**Opening a new Tab in Selenium 4**

    

```
driver.get("https://www.google.com/");
// Opens a new window and switches to new window
driver.switchTo().newWindow(WindowType.TAB);
// Opens LambdaTest homepage in the newly opened tab
driver.navigate().to("https://www.lambdatest.com/");
```

## Deprecation Of Desired Capabilities

In Selenium 4, Desired Capabilities are getting replaced with Options, and we need to create an Options object to use the Driver class.

The below-mentioned Options objects will be used in Selenium 4 for defining the browser-specific capabilities.

* Chrome — ChromeOptions

* Firefox — FirefoxOptions

* Safari — SafariOptions

* Internet Explorer (IE) — InternetExplorerOptions

* Microsoft Edge — EdgeOptions

## Chrome DevTools Protocol (CDP)

Chrome DevTools Protocol is the new addition in Selenium 4. The new feature provides Chrome and Microsoft Edge’s debugging protocol for debugging, simulating poor network conditions, and emulating geolocation for automation testing.

[Geolocation testing with Selenium](https://www.lambdatest.com/blog/geolocation-testing-with-selenium-using-examples/?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog) helps in mocking browser’s geolocation for the purpose of web browser testing. This feature in Selenium 4 can be leveraged to test the website’s functionalities by mocking different geographical locations.

{% youtube https://youtu.be/c08Z3HoSDCM?utm_source=devto&utm_medium=organic&utm_campaign=apr13_pk&utm_term=pk&utm_content=blog  %}

## Conclusion

In this Selenium WebDriver tutorial, I deep-dived into the basics of Selenium WebDriver, its components, and Selenium WebDriver architecture. I also implemented test scenarios using WebDriver as well as cloud Selenium Grid. In the end, we also glanced upon the new and improved features which Selenium 4 offers. I believe you are now all geared up to start on your Selenium automation journey.

Happy testing! 🙂