Skip to main content
Beginner to Advanced · Selenium 4 · Java

Selenium Tutorial for Beginners: Learn WebDriver with Java

New to automation? Start here. Learn what Selenium is, write your first script, then go step by step to locators, waits, Page Object Model and running tests in parallel. Every example is real Java code you can run against our free practice websites.

✍️ Written byShivam Singh•Founder & QA Engineer•✓ Verified on Selenium 4.25+•Updated: October 2026
Author:TesterRank Lead SDET Team
Updated:October 2026 Edition
Stack:Selenium 4.25+ • Java 17/21 • TestNG
Depth:2,800+ Words • Production Code

Start Here: What Is Selenium?

Selenium is a free tool that lets you control a web browser with code. Anything you do by hand while testing a website (open a page, type a username, click Login, check the welcome message) you can write once as a Selenium script and then run hundreds of times, on any browser, in a few minutes.

Think of Selenium as a robot sitting at your keyboard. You tell it, in Java, what to click and what to check, and it does the job exactly the same way every time. That is why companies use it for regression testing (re-checking old features after every new change).

Selenium WebDriver

The main part. A Java library that sends commands to Chrome, Firefox, Edge or Safari.

Selenium IDE

A browser extension that records your clicks. Good for a quick demo, not for real projects.

Selenium Grid

Runs many tests at the same time on many machines or browsers.

What you need before you start

  • Basic Java: variables, if/else, loops, methods and classes. (Free Java for Testers path)
  • Java 17 or newer, Maven, IntelliJ IDEA Community (all free) and Google Chrome.
  • No paid tools and no special computer. A normal laptop is enough.

How to Learn Selenium in 6 Steps

Follow these steps in order. Each one builds on the one before it, and each has a free lesson or practice page on TesterRank.

  1. 1

    Learn basic Java

    Only what testers need: variables, loops, methods, classes and collections.

    Java for Testers →
  2. 2

    Open a browser with code

    Launch Chrome, open a URL, go back and forward, read the page title.

    First WebDriver lesson →
  3. 3

    Find elements with locators

    Use id, CSS selectors and XPath to point at buttons, fields and links.

    Locators lesson →
  4. 4

    Handle waits

    Make tests wait for the page instead of failing at random.

    Explicit waits lesson →
  5. 5

    Organise tests with TestNG and Page Object Model

    Group tests, add assertions and keep locators in one place.

    TestNG path →
  6. 6

    Practise on realistic websites

    Automate login, transfers and checkout on demo apps built for practice.

    Banking practice portal →

Want a day-by-day plan? The free 45-Day Study Plan covers all six steps with daily tasks.

Your First Selenium Script

This short program opens Chrome, logs in to the TesterRank practice bank with the demo account and prints the page title. Add the Selenium dependency from step 2 below to your Maven project, then run it.

FirstSeleniumTest.javaJava
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class FirstSeleniumTest {
    public static void main(String[] args) {
        WebDriver driver = new ChromeDriver();                    // 1. Open Chrome
        driver.get("https://www.testerrank.com/banking/login");   // 2. Go to the login page

        driver.findElement(By.cssSelector("[data-testid='userId']")).sendKeys("rahul@netbank.com"); // 3. Type email
        driver.findElement(By.cssSelector("[data-testid='password']")).sendKeys("Bank@123");      // 4. Type password
        driver.findElement(By.cssSelector("[data-testid='loginBtn']")).click();                     // 5. Click Login

        System.out.println("Page title: " + driver.getTitle());   // 6. Check where we landed
        driver.quit();                                            // 7. Close the browser
    }
}
  • new ChromeDriver() opens a new Chrome window. Selenium downloads the right driver for you.
  • findElement(By...) finds one element on the page using a locator.
  • sendKeys() types text and click() clicks.
  • quit() closes the browser. Always call it, or old windows pile up.

That is the whole idea of Selenium. The rest of this tutorial goes deeper: how Selenium talks to the browser, choosing stable locators, waits, Page Object Model and running tests in parallel.

1. Selenium 4 Architecture & The W3C Standard

For over a decade, Selenium 2 and 3 operated on the JSON Wire Protocol. In that legacy architecture, every command sent from your Java test (like driver.findElement()) had to be serialized into HTTP JSON packets, shipped across a network boundary, decoded by a standalone browser driver (like ChromeDriver or IEDriver), and finally translated into browser-specific commands.

With Selenium 4, the entire architecture was rebuilt around the official W3C WebDriver Specification. Modern browsers (Chrome, Firefox, Safari, Edge) implement this standard natively. Because client bindings and browser drivers speak the identical W3C dialect, there is zero encoding/decoding overhead. Tests execute faster, return more consistent error payloads, and unlock modern capabilities like the Bidirectional (BiDi) Protocol.

W3C WebDriver vs Legacy JSON Wire Protocol

Selenium 3 (Legacy)

Java Client → [JSON Wire Encoding] → HTTP REST Request → [JSON Wire Decoding] → Browser Driver → Browser. High flakiness and protocol translation bottlenecks.

Selenium 4 (Modern W3C)

Java Client → [Direct W3C WebDriver Standard Calls] → Browser Driver → Browser. Direct communication plus native Chrome DevTools Protocol (CDP) support for network & console logs.

2. Maven Dependencies & Selenium Manager

Setting up an enterprise test automation project starts with a clean pom.xml file. In previous Selenium versions, engineers had to include third-party libraries like Bonigarcia WebDriverManager or manually download browser executable files. In Selenium 4.6+, Selenium includes an automated driver engine called Selenium Manager out of the box.

pom.xmlHTML/XML
<dependencies>
    <!-- Selenium Java Language Bindings (Selenium 4) -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.25.0</version>
    </dependency>

    <!-- TestNG Test Runner Framework -->
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.10.2</version>
        <scope>test</scope>
    </dependency>

    <!-- SLF4J Simple Logger (Prevents framework logging warnings) -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>2.0.16</version>
        <scope>test</scope>
    </dependency>
</dependencies>

💡 SDET Tip: Always scope your test dependencies with <scope>test</scope> so test libraries are not bundled into production binaries when building shared Java packages.

3. Browser Initialization & Enterprise ChromeOptions

Running tests on a developer laptop with a visible UI is convenient for debugging. However, production CI/CD pipelines run in headless Linux Docker containers without an X11 graphical display. Using standard new ChromeDriver() without configuration frequently causes memory crashes in CI due to shared memory limits (/dev/shm).

DriverFactory.javaJava
package com.testerrank.driver;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.time.Duration;

public class DriverFactory {

    public static WebDriver createChromeDriver(boolean isHeadless) {
        ChromeOptions options = new ChromeOptions();

        if (isHeadless) {
            // Chrome 109+ new headless mode: runs the full browser engine
            options.addArguments("--headless=new");
            // Mandatory for Docker/Linux environments to avoid /dev/shm OOM crashes
            options.addArguments("--disable-dev-shm-usage");
            options.addArguments("--no-sandbox");
            options.addArguments("--disable-gpu");
        }

        // Standardize viewport so tests do not fail on responsive layouts
        options.addArguments("--window-size=1920,1080");
        options.addArguments("--disable-notifications");

        WebDriver driver = new ChromeDriver(options);
        
        // Page load timeout prevents hanging on slow third-party scripts
        driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(30));
        
        // ZERO implicit wait by default to avoid race conditions
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(0));

        return driver;
    }
}

4. Enterprise Locator Strategies: CSS vs XPath vs Test-IDs

Brittle locators are the number one cause of flaky test suites. When a frontend developer refactors an HTML structure or wraps a form field in an extra <div>, fragile tests fail immediately. Here is the industry hierarchy of locator stability:

PriorityStrategyExample LocatorWhen to Use
1 (Best)Unique Test IDsBy.cssSelector("[data-testid='submit-btn']")Dedicated automation hooks immune to layout refactors
2Static Semantic IDBy.id("userId")Standard form elements with unique, server-rendered IDs
3CSS SelectorsBy.cssSelector("form.login-card input[type='password']")Fast DOM traversal using native browser query engines
4Relative XPathBy.xpath("//label[text()='Email']/following-sibling::input")Traversing parent/sibling relationships or matching text
5 (Avoid)Absolute XPathBy.xpath("/html/body/div[2]/div[1]/form/input[1]")Never use. Breaks on any DOM hierarchy modification

5. Synchronization & Wait Strategies: Eliminating Flakiness

Modern single-page applications (React, Next.js, Vue) fetch data asynchronously via REST/GraphQL. When a user clicks a button, the page does not reload — parts of the DOM update dynamically. If your script searches for an element before the browser has finished rendering it, Selenium throws a NoSuchElementException or ElementNotInteractableException.

WaitComparison.javaJava
// ❌ NEVER USE Thread.sleep() — it pauses the JVM blindly, wasting CI minutes
// Thread.sleep(5000);

// ✅ Standard Explicit Wait: Polls every 500ms until condition is satisfied or timeout
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement transferBtn = wait.until(
    ExpectedConditions.elementToBeClickable(By.id("confirmTransferBtn"))
);
transferBtn.click();

// ✅ Advanced Fluent Wait: Custom polling frequency & exception filtering
Wait<WebDriver> fluentWait = new FluentWait<>(driver)
    .withTimeout(Duration.ofSeconds(15))
    .pollingEvery(Duration.ofMillis(250))
    .ignoring(NoSuchElementException.class)
    .ignoring(StaleElementReferenceException.class);

WebElement balanceBadge = fluentWait.until(d -> {
    WebElement el = d.findElement(By.id("accountBalance"));
    return el.getText().contains("$") ? el : null;
});

6. End-to-End Test: NetBanking Login & Transfer Flow

Let's automate a real end-to-end test against TesterRank's live ISB NetBanking Portal. This script demonstrates browser launch, form filling, explicit waiting for modal state, and assertion verification using TestNG.

BankingLoginTest.javaJava
package com.testerrank.tests;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.time.Duration;

public class BankingLoginTest {

    private WebDriver driver;
    private WebDriverWait wait;

    @BeforeMethod
    public void setUp() {
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    }

    @Test(description = "Verify successful user login into ISB NetBanking Portal")
    public void testValidBankingLogin() {
        // Navigate to the live TesterRank Banking Portal
        driver.get("https://www.testerrank.com/banking/login");

        // Wait for login inputs to become visible
        WebElement userIdInput = wait.until(
            ExpectedConditions.visibilityOfElementLocated(By.id("userId"))
        );
        WebElement passwordInput = driver.findElement(By.id("password"));
        WebElement loginButton = driver.findElement(By.id("loginBtn"));

        // Fill credentials and click sign in
        userIdInput.sendKeys("user101");
        passwordInput.sendKeys("BankingPass@2026");
        loginButton.click();

        // Wait for dashboard account header to load
        WebElement dashboardHeader = wait.until(
            ExpectedConditions.visibilityOfElementLocated(By.id("accountOverviewHeader"))
        );

        // Assert that the user is authenticated successfully
        Assert.assertTrue(dashboardHeader.isDisplayed(), "Dashboard header should be visible upon login");
        Assert.assertTrue(driver.getCurrentUrl().contains("/banking"), "User should remain in banking domain");
    }

    @AfterMethod
    public void tearDown() {
        if (driver != null) {
            driver.quit(); // Closes all browser windows and kills driver process
        }
    }
}

7. Page Object Model (POM): Enterprise Design Pattern

In real engineering teams, writing locators directly inside test methods creates massive maintenance overhead. When a login button ID changes, you would have to update 50 different test files. The Page Object Model (POM) solves this by creating a dedicated class for each web page that encapsulates its locators and user actions.

LoginPage.javaJava
public class LoginPage {
    private final WebDriver driver;
    private final WebDriverWait wait;

    // Locators kept private to this page
    private final By userIdField = By.id("userId");
    private final By passwordField = By.id("password");
    private final By loginBtn = By.id("loginBtn");

    public LoginPage(WebDriver driver) {
        this.driver = driver;
        this.wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    }

    public DashboardPage login(String user, String pass) {
        wait.until(ExpectedConditions.visibilityOfElementLocated(userIdField)).sendKeys(user);
        driver.findElement(passwordField).sendKeys(pass);
        driver.findElement(loginBtn).click();
        return new DashboardPage(driver);
    }
}
LoginScenarioTest.javaJava
public class LoginScenarioTest extends BaseTest {

    @Test
    public void verifyAuthorizedLogin() {
        driver.get("https://www.testerrank.com/banking/login");

        LoginPage loginPage = new LoginPage(driver);
        DashboardPage dashboard = loginPage.login("user101", "BankingPass@2026");

        Assert.assertTrue(
            dashboard.isAccountOverviewVisible(),
            "Expected user dashboard to render after sign in"
        );
    }
}

8. Diagnosing Common Production Exceptions

When running thousands of automated tests nightly, you will encounter standard WebDriver exceptions. Here is how senior SDETs diagnose and remediate them:

1. StaleElementReferenceException

Root Cause: The element was located, but JavaScript re-rendered that portion of the DOM (e.g. React virtual DOM reconciliation), invalidating the internal node reference ID stored by WebDriver.

Fix: Do not cache WebElements in class fields. Re-query using driver.findElement(by) right at interaction time, or use ExpectedConditions.refreshed(...).

2. ElementClickInterceptedException

Root Cause: The target button is in the DOM, but another element (like a sticky header, toast banner, or loading spinner overlay) is physically covering its coordinates at click time.

Fix: Wait for loading spinners to disappear: wait.until(ExpectedConditions.invisibilityOfElementLocated(By.className("spinner"))), or scroll element into center viewport before clicking.

3. NoSuchElementException

Root Cause: WebDriver polled the DOM immediately before the server response arrived or element finished rendering.

Fix: Replace direct lookups with wait.until(ExpectedConditions.visibilityOfElementLocated(locator)).

9. Thread-Safe Parallel Execution with ThreadLocal WebDriver

Sequential test runs take hours. Running 200 tests across 4 parallel threads can cut execution time from 40 minutes to under 10 minutes. However, if your tests share a static public static WebDriver driver, thread collisions will cause one test to navigate while another test is clicking, causing widespread test failure.

BaseTest.javaJava
public class BaseTest {
    // ThreadLocal ensures every thread maintains its own isolated WebDriver instance
    private static final ThreadLocal<WebDriver> driverThread = new ThreadLocal<>();

    public static WebDriver getDriver() {
        return driverThread.get();
    }

    @BeforeMethod
    public void setUp() {
        WebDriver driver = DriverFactory.createChromeDriver(true);
        driverThread.set(driver);
    }

    @AfterMethod
    public void tearDown() {
        WebDriver driver = driverThread.get();
        if (driver != null) {
            driver.quit();
            driverThread.remove(); // Prevents memory leaks in CI agent worker pools
        }
    }
}

10. Frequently Asked Questions (FAQ)

Is Selenium easy to learn for a manual tester?

Yes. You need basic Java (variables, if/else, loops, methods and classes), which most people learn in 2 to 3 weeks. After that, Selenium itself is a small set of commands: open a page, find an element, click, type and check the result. Practising daily on real websites is what makes it stick.

How long does it take to learn Selenium?

Most learners write useful scripts in about 4 weeks and build a full framework (TestNG, Page Object Model, reports) in 6 to 8 weeks, studying 1 to 2 hours a day. The free TesterRank 45-Day Study Plan follows exactly this order.

Should I learn Selenium with Java or Python?

For jobs in India, Java is the safer choice: most QA job posts ask for Selenium with Java, TestNG and Maven. Python is shorter to write and also good. The Selenium commands are almost the same in both, so you can switch later.

Is Selenium free?

Yes. Selenium is open source and completely free. Java, Maven, TestNG, IntelliJ IDEA Community Edition and the Chrome browser are free too, so you can learn and practise without paying anything.

Should I learn Selenium or Playwright first?

Learn Selenium first if you are job hunting: it is still the most asked-for tool in interviews. Playwright is newer and faster, and easy to pick up once you know Selenium, because the ideas (locators, waits, assertions) are the same.

What is the main architectural difference between Selenium 3 and Selenium 4?

Selenium 3 relied on the JSON Wire Protocol, requiring requests from client language bindings to be encoded and decoded through an intermediate HTTP JSON protocol before reaching browser drivers. Selenium 4 is fully W3C WebDriver compliant from end-to-end. Client bindings communicate directly with browser drivers (ChromeDriver, GeckoDriver) using the standardized W3C protocol, eliminating encoding overhead, improving test execution speed, and unlocking native Bidirectional (BiDi) capabilities via Chrome DevTools Protocol (CDP).

Why should you never mix Implicit Wait and Explicit Wait in Selenium?

Mixing Implicit Wait and Explicit Wait produces undefined timeout behaviors under the W3C WebDriver specification. An implicit wait applies globally to all element lookups on the driver instance, while an explicit wait evaluates conditions dynamically. When combined, a failing lookup can cause unexpected cumulative delays (e.g., an explicit wait of 10s plus an implicit wait of 10s may hang for 20s or time out intermittently). Industry best practice is to set implicit wait to zero and exclusively use WebDriverWait with targeted ExpectedConditions.

How do you recover from a StaleElementReferenceException in Selenium?

A StaleElementReferenceException occurs when a previously located WebElement is no longer attached to the DOM (for example, after an AJAX page refresh, component re-render in React/Angular, or DOM tree rebuilding). To fix it: 1) Re-locate the element immediately prior to interaction rather than caching WebElement references, 2) Use WebDriverWait with ExpectedConditions.refreshed(ExpectedConditions.elementToBeClickable(locator)), or 3) Employ the Page Object Model with By locators rather than pre-initialized WebElements.

How does Selenium Manager eliminate third-party WebDriverManager in Selenium 4.6+?

Starting with Selenium 4.6, Selenium ships with an embedded Rust-based binary called Selenium Manager. When your script instantiates `new ChromeDriver()` without specifying a driver system property, Selenium Manager automatically inspects your installed Chrome browser version, downloads the matching driver binary from Google endpoints, and caches it locally. You no longer need `io.github.bonigarcia.webdrivermanager` or manual `System.setProperty()` calls in your framework.

How do you execute Selenium tests in parallel safely in CI/CD pipelines?

Parallel test execution requires complete isolation of the WebDriver instance per test thread. In Java, this is achieved by storing the WebDriver in a `ThreadLocal<WebDriver>` container inside a BaseTest class. In TestNG, configure `parallel="methods"` or `parallel="classes"` with `thread-count="4"` in `testng.xml`. Each thread gets its own browser session, preventing race conditions, overlapping cookies, and session collision.

Ready to Practice Real Automation?

Knowledge without hands-on practice does not build automation confidence. Launch our 10 realistic practice applications and automate real forms, tables, and checkout funnels.