Software Testing
Getting Started with Java Selenium: Build Your First Automation Script
Learn how to set up Java, Selenium WebDriver, and Maven from scratch. Step-by-step guide to writing and running your first automated test script.

Hover to zoomEvery manual tester reaches a point where clicking the same login button for the hundredth time feels like a waste of human potential. That is where getting started with Java Selenium comes in. By pairing Java—the enterprise standard programming language—with Selenium WebDriver, you can automate repetitive browser tasks, validate UI changes, and build robust regression suites that run while you sleep.
In this guide, we will bypass the outdated setup tutorials that force you to download zip files and configure environment variables manually. Instead, we will use modern tools like Apache Maven to manage our dependencies and get a clean, working automation script running in under ten minutes.
Why Pair Java with Selenium and Maven?
Java remains the most popular language for Selenium automation. Most enterprise applications are built on Java, making it easier for development and QA teams to collaborate on the same codebase. When you combine Java with Selenium WebDriver, you get access to a massive community, mature testing frameworks like TestNG or JUnit, and excellent IDE support.
But managing raw JAR files is a nightmare. That is why we use Maven. Maven is a build automation tool that handles your project's lifecycle and external libraries. Instead of hunting down JARs on the internet, you simply declare your dependencies in a single XML file, and Maven handles the rest.
| Manual JAR Management | Maven Dependency Management | |
|---|---|---|
| Setup Time | Slow (manual download & classpath config) | Fast (defined in pom.xml) |
| Version Control | Difficult to track and update | Easy (change version number in XML) |
| Team Collaboration | Must share raw files or commit JARs to Git | Share light XML configuration file |
| Transitive Dependencies | Must resolve and download manually | Resolved and downloaded automatically |
Step 1: Setting Up Your Maven Project
Before writing code, we need to create a structured project. Open your preferred IDE (we highly recommend IntelliJ IDEA Community Edition) and create a new Maven project. Select the default archetype and name your project something descriptive, like `SeleniumAutomationDemo`.
Once your project is created, locate the `pom.xml` file in the root directory. This Project Object Model file is where we tell Maven which libraries we need. We will add dependencies for Selenium Java and WebDriverManager.
<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.squarebrackets</groupId>
<artifactId>selenium-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- Selenium Java Dependency -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.18.1</version>
</dependency>
<!-- WebDriverManager for automatic driver management -->
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.7.0</version>
</dependency>
</dependencies>
</project>After updating your `pom.xml`, make sure to reload the Maven changes. In IntelliJ, you can do this by clicking the small Maven icon that pops up, or by pressing `Ctrl+Shift+O` (`Cmd+Shift+I` on Mac).
Step 2: Understanding the WebDriver Architecture
How does your Java code actually control a browser like Chrome or Firefox? It happens through a three-tier architecture. Your Java code communicates with the Selenium WebDriver API. WebDriver then sends commands (using the W3C WebDriver protocol) to a browser-specific driver, such as ChromeDriver or GeckoDriver. Finally, the driver executes these commands directly on the browser.
- Your Code: Written in Java using the Selenium API.
- WebDriver API: Standardizes browser interactions into generic commands.
- Browser Drivers: Executable files (like chromedriver.exe) that act as translators.
- Real Browser: Chrome, Firefox, or Edge executing the actions.
Step 3: Writing Your First Selenium Automation Script
Now, let us write a simple test script. Navigate to `src/main/java` in your project folder, create a new package named `com.demo`, and create a Java class named `FirstTest.java`. We will write a script that opens Google, searches for 'Squarebrackets Noida', and verifies the page title.
package com.demo;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class FirstTest {
public static void main(String[] args) {
// Setup ChromeDriver automatically
WebDriverManager.chromedriver().setup();
// Initialize WebDriver instance
WebDriver driver = new ChromeDriver();
try {
// 1. Navigate to Google
driver.get("https://www.google.com");
driver.manage().window().maximize();
// 2. Locate the search box using its name attribute
WebElement searchBox = driver.findElement(By.name("q"));
// 3. Type search query and press Enter
searchBox.sendKeys("Squarebrackets Online Training");
searchBox.sendKeys(Keys.ENTER);
// 4. Wait briefly and print the page title
Thread.sleep(2000);
System.out.println("Page Title is: " + driver.getTitle());
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
// 5. Always close the browser session
driver.quit();
}
}
}Step 4: Running and Debugging Your Script
To run your script, right-click inside the `FirstTest` class in your IDE and select Run 'FirstTest.main()'. You should see a Chrome window open, navigate to Google, type the search query, press enter, and then close automatically. The console output will display the title of the search results page.
If your script fails with a `NoSuchElementException`, it means Selenium could not locate the HTML element with the locator you provided. Use Chrome DevTools (right-click on any element on a webpage and select Inspect) to verify element IDs, classes, or names before writing your locators.
Getting started with Java Selenium is easier than it looks when you use modern tools like Maven and WebDriverManager. You have successfully configured your environment, managed dependencies without downloading manual JARs, and written a script to automate a real browser session. From here, you can dive deeper by joining our structured <a href="/automation-java-selenium">Automation with Java & Selenium</a> course, or explore testing frameworks with our guide on <a href="/blogs/junit-vs-testng">JUnit vs TestNG</a> to build production-grade test suites.
Getting started with Java Selenium is easier than it looks when you use modern tools like Maven and WebDriverManager. You have successfully configured your environment, managed dependencies without downloading manual JARs, and written a script to automate a real browser session. From here, you can explore advanced topics like the Page Object Model (POM), explicit waits, and integration with testing frameworks like TestNG to build production-grade test suites.
