Loading

Loading…

Selenium WebDriver for Beginners: Set Up Java, Maven and Your First Script

New to automation? Install Java and IntelliJ, create a Maven project, add Selenium WebDriver and run your first script that logs in to a real practice site.

QAliber TeamOctober 5, 2026
Selenium WebDriver for Beginners: Set Up Java, Maven and Your First Script

You have decided to learn automation. You open a Selenium tutorial, and within five minutes it asks you to set a PATH variable, download a driver, and fix a version mismatch error you don't understand. Many manual testers give up right here, before writing a single line of test code.

Setup is much easier today than it used to be. This guide walks you through it step by step, using Java, IntelliJ IDEA and Maven, and ends with a working script that logs in to a real practice site.

Why Java for Selenium?

Selenium supports Java, Python, C#, JavaScript and Ruby. Java is still a very common choice in QA teams in India, and many job descriptions ask for Selenium with Java. If your team uses another language, the concepts in this post still apply.

Step 1: Install the Tools

Java Development Kit (JDK)

Download a current LTS version of the JDK, such as from Eclipse Temurin (Adoptium) or Oracle. After installing, open a terminal or command prompt and check:

```bash
java -version
```

If you see a version number, Java is installed.

IntelliJ IDEA Community Edition

IntelliJ is a free, beginner-friendly editor for Java. Download the Community Edition from JetBrains. It includes Maven support, so you don't need to install Maven separately for this guide.

Google Chrome

You probably have it already. Selenium will use your installed Chrome.

Step 2: Create a Maven Project

Maven is a build tool. For a beginner, its most useful job is this: you list the libraries you need in one file, and Maven downloads them for you.

In IntelliJ:

  1. Click New Project.
  2. Choose Java on the left, and select Maven as the build system.
  3. Choose your installed JDK.
  4. Give it a name like selenium-basics and click Create.

You will see a structure like this:

```
selenium-basics/
├── pom.xml
└── src/
├── main/java/
└── test/java/
```

The pom.xml file is where you add Selenium.

Step 3: Add Selenium to pom.xml

Open pom.xml and add a dependencies section inside the <project> tag:

```xml
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.x.x</version>
</dependency>
</dependencies>
```

Replace 4.x.x with the latest Selenium 4 version listed on Maven Central (search for "selenium-java"). Then click the small Maven refresh icon that appears in IntelliJ. Maven downloads Selenium and everything it needs.

Step 4: What About ChromeDriver?

Older tutorials tell you to download ChromeDriver and set its path. Since Selenium 4.6, a built-in tool called Selenium Manager finds or downloads the correct driver for your browser automatically in most setups.

So for this guide, you do not need to download any driver. If your company network blocks downloads, you may still need to set it up manually. Ask your team, as this is a common office setup issue.

Step 5: Write Your First Script

We will use Sauce Demo, a public practice site made for testers. The test username standard_user and password secret_sauce are shown on its login page.

Create a new Java class under src/test/java called FirstScript:

```java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class FirstScript {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();

driver.manage().window().maximize();
driver.get("https://www.saucedemo.com");

driver.findElement(By.id("user-name")).sendKeys("standard_user");
driver.findElement(By.id("password")).sendKeys("secret_sauce");
driver.findElement(By.id("login-button")).click();

String heading = driver.findElement(By.className("title")).getText();
System.out.println("Page heading after login: " + heading);
System.out.println("Current URL: " + driver.getCurrentUrl());

driver.quit();
}
}
```

Click the green Run arrow next to main. Chrome opens, the script logs in, prints the heading and URL, and closes the browser.

Congratulations, that is your first automation script.

What Each Line Does

  • WebDriver driver = new ChromeDriver(); starts a new Chrome browser controlled by Selenium. WebDriver is the interface; ChromeDriver is the Chrome implementation.
  • driver.manage().window().maximize(); maximises the window, so elements are less likely to be hidden.
  • driver.get(url) opens the page and waits for it to load.
  • driver.findElement(By.id(...)) finds an element on the page. Here we use its id.
  • sendKeys(...) types text into a field.
  • click() clicks an element.
  • getText() reads the visible text of an element.
  • driver.quit() closes the browser and ends the session. Always call it at the end, or you will have many leftover browser windows.

How This Maps to a Manual Test Case

| Manual step | Selenium code |
|---|---|
| Open the login page | driver.get(...) |
| Enter username | findElement(...).sendKeys(...) |
| Enter password | findElement(...).sendKeys(...) |
| Click Login | findElement(...).click() |
| Check the Products page opens | getText() and compare |

Right now we only print the result. In the last post of this series, we will turn this into a real test with assertions using TestNG.

Common Setup Errors and Fixes

"Cannot resolve symbol WebDriver"

Maven has not downloaded Selenium yet. Click the Maven refresh icon, or open the Maven panel and click Reload All Maven Projects. Check your internet or proxy settings if it still fails.

SessionNotCreatedException

This usually means a mismatch between the browser and driver versions. With Selenium Manager, updating Selenium to the latest 4.x version usually fixes it. Also make sure Chrome itself is up to date.

NoSuchElementException

Selenium could not find the element. Check the locator in Chrome DevTools, and check that the page has finished loading. We cover locators and waits in the next posts.

Browser Opens and Closes Immediately

That is expected. driver.quit() closes it. To look at the page, temporarily comment out quit(), or add a breakpoint and run in debug mode.

Try It Yourself

Before moving on, practise by changing the script:

  1. Log in with a wrong password and print the error message. Hint: the error element has the attribute data-test="error", so you can use By.cssSelector("[data-test='error']").
  2. After logging in, click the cart icon and print the page heading.
  3. Use driver.navigate().back() and print the URL again.

Small changes like these build confidence quickly.

Takeaway

Setting up Selenium WebDriver today means installing Java and IntelliJ, creating a Maven project and adding one dependency. Selenium Manager takes care of the driver. With one short script, you have already automated a real login flow.

Getting stuck on setup or your first script? Join the QAliber community and get help from testers who made the same move into automation.