Run Your First Selenium Script in Java

Welcome to the world of Test Automation!

If you are reading this, you are likely standing at the edge of manual testing, looking to leap into automation. Writing your first script is a rite of passage. It changes how you view software quality – shifting from repetitive manual clicking to building intelligent “robots” that do the work for you.

This guide is designed for absolute beginners. We will move slowly, explain every concept, and use the modern approach (Selenium 4.x) that avoids the complex configurations of the past.

By the end of this article, you will have a working script that launches a browser, visits a live practice site, and interacts with it.

Before we write code, let’s understand the tool.

Selenium is the industry-standard library for automating web browsers. It allows you to write code in languages like Java, Python, or C# to control a browser (Chrome, Firefox, Edge, etc.) just like a human user would. This article would write the code in Java, though !

If you have looked at older tutorials, you might have seen instructions to manually download a file called chromedriver.exe and set “System Paths.” Forget that.

We will use newer Selenium versions. Newer version includes a smart feature called Selenium Manager. It automatically detects your browser version and downloads the correct driver for you in the background. It makes the setup process zero-configuration.

To build this script, you need three standard tools installed on your machine.

  • Java Development Kit (JDK): Selenium is a Java library, so we need Java installed. (Version 11 or higher is recommended).
  • An IDE (Code Editor): This is where you write your code. IntelliJ IDEA (Community Edition) is highly recommended for beginners, though Eclipse works as well.

We won’t just write a file; we will create a proper Maven Project. This is the standard way automation frameworks are built in the industry.

  • Open your IDE (e.g., IntelliJ).
  • Select File > New > Project
Create new project
  • Choose Maven as the build type and name the project FirstSeleniumScript.
Maven project
  • Click Create.

Here’s how the project structure will look:

Maven project structure

Right now, your project is empty. It doesn’t know what “Selenium” is. We need to tell Maven to go to the internet, grab the Selenium code, and add it to our project.

  • Look for a file named pom.xml in your project folder. Open it.
pom.xml file
  • Look for the <dependencies> tag. If you don’t see it, create it inside the main <project> tags.
  • Paste the following code inside the dependencies section:
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>4.39.0</version>
</dependency>

Here is the full pom.xml file –

<?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>FirstSeleniumScript</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>25</maven.compiler.source>
        <maven.compiler.target>25</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.39.0</version>
        </dependency>
    </dependencies>

</project>

Crucial Step:

After adding the Selenium dependency to the pom.xml file, right-click on the project, select Maven, and then click Sync Project.

That’s it! Selenium is now configured and ready to use in your project.

Before we look at the code, let’s clearly define what our “Selenium script” is going to do today. We are going to automate a simple verification flow on a live practice website.

The Script Steps:

  1. Open Chrome: Launch a fresh browser window.
  2. Go to TestKru: Navigate to https://testkru.com/Elements/TextFields.
  3. Get Title: Ask the browser, “What is the title of this page?”
  4. Verify: Print that title to our console so we can confirm we are on the right page.
  5. Clean Up: Close the browser to free up memory.

We will write this script piece by piece so you understand the logic.

  1. Navigate to the src > main > java folder in your project.
  2. Right-click and create a new Java Class named GetPageTitle.
Add a new class "GetPageTitle"

Now, let’s add the code inside the main() method of the GetPageTitle class.

GetPageTitle class

The first thing any script needs is a WebDriver object.

// We initialize the ChromeDriver. 
// Thanks to Selenium Manager, this automatically sets up the browser binary.
WebDriver driver = new ChromeDriver();

We use the .get() command to open a URL. In this example, we’ll use TestKru, a practice website designed for automation testers. Don’t worry about how the get() method works, just remember that it launches the browser and opens the URL passed to it.

// Open the TestKru TextFields practice page
driver.get("https://testkru.com/Elements/TextFields");

Now that the page is open, let’s get some information from it. We will fetch the Page Title (the text you see on the browser tab). This is often used to verify that we landed on the correct page.

The highlighted text represents the title of the webpage that was opened.

title of text field page
// Capture the title and store it in a String variable
String title = driver.getTitle();

// Print it to our console so we can see it
System.out.println("Page Title is: " + title);

This is the most important step for a beginner. If you don’t close the browser via code, your computer memory will fill up with “zombie” browser processes.

// Quit the session and close all windows
driver.quit();

Here is the full code, assembled together. Copy and paste this into your GetPageTitle.java file.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class GetPageTitle {
    static void main(String[] args) {

        // 1. Launch the Browser
        // Selenium Manager automatically handles the driver setup
        WebDriver driver = new ChromeDriver();

        try {
            // 2. Navigate to the URL
            // We use the TestKru practice page
            driver.get("https://testkru.com/Elements/TextFields");

            // 3. Get the Title of the Page
            // This captures the tab name of the current page
            String pageTitle = driver.getTitle();

            // 4. Print the output
            System.out.println("----------------------------------------");
            System.out.println("Successfully accessed: " + pageTitle);
            System.out.println("----------------------------------------");

        } catch (Exception e) {
            // If anything goes wrong, print the error
            e.printStackTrace();
        } finally {
            // 5. Close the Browser
            // We put this in a 'finally' block to ensure the browser 
            // closes even if the test fails halfway through.
            driver.quit();
        }
    }
}
  • Right-click anywhere inside your Java code.
  • Select Run ‘GetPageTitle.main()’.

What will happen?

  1. Compilation: Java will check your code for errors.
  2. Launch: A Chrome window will suddenly pop up.
  3. Execution: You will see the TestKru website load.
  4. Completion: The browser will close immediately after loading.

Check your Console Output: Look at the bottom of your IDE. You should see a message like this:

----------------------------------------
Successfully accessed: Text Fields
----------------------------------------

Congratulations! You have just written your first automation script.

Let us know in the comments if you run into any issues while following the steps in this article or while executing the script.

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *