Skip to main content

How to Write Your First Selenium Test Script

If you’re new to test automation, Selenium is one of the most popular tools to get started with. It allows you to automate browser actions, making it easier to test web applications efficiently. In this blog, we’ll walk through how to write your very first Selenium test script step-by-step.

What is Selenium?

Selenium is an open-source framework used for automating web browsers. It supports multiple programming languages like Java, Python, C#, and JavaScript, and works with all major browsers including Chrome, Firefox, and Edge.

Using Selenium, you can simulate user interactions like clicking buttons, entering text, and navigating between pages—all automatically.

Prerequisites

Before writing your first Selenium test, make sure you have the following installed:

  • Java Development Kit (JDK) (if you choose Java)

  • An Integrated Development Environment (IDE) like IntelliJ IDEA or Eclipse

  • Selenium WebDriver libraries (which you can add as dependencies)

  • A browser driver executable (e.g., ChromeDriver for Chrome)

Note: This tutorial will focus on Java, but Selenium supports other languages too.

Setting Up Your Project

  • Create a new Java project in your IDE.
  • Add Selenium WebDriver to your project dependencies.
If you use Maven, add this to your pom.xml:
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>4.10.0</version>
</dependency>
  • Download the browser driver.

For Chrome, download ChromeDriver from here.

  • Set the path to your driver in your test script. 

Writing Your First Selenium Test Script

Let’s create a basic script that:

  • Launches the Chrome browser

  • Navigates to a website

  • Prints the page title

  • Closes the browser

Here’s the complete Java code:

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

public class FirstSeleniumTest {
    public static void main(String[] args) {
        // Set the path to the ChromeDriver executable
        System.setProperty("webdriver.chrome.driver", "C:/path/to/chromedriver.exe");

        // Create a new instance of the ChromeDriver
        WebDriver driver = new ChromeDriver();

        // Navigate to a website
        driver.get("https://www.example.com");

        // Print the title of the page
        System.out.println("Page title is: " + driver.getTitle());

        // Close the browser
        driver.quit();
    }
}

Code Breakdown

Let’s go through this line by line:

System.setProperty("webdriver.chrome.driver", "C:/path/to/chromedriver.exe");
This tells Selenium where your ChromeDriver is located. Make sure to update the path to where you saved chromedriver.exe

WebDriver driver = new ChromeDriver();
This line launches a new Chrome browser instance.

driver.get("https://www.example.com");
Navigates to the specified URL.

System.out.println("Page title is: " + driver.getTitle());
Prints the page title to the console—great for confirming your test is working.

driver.quit();
Closes the browser and ends the WebDriver session.

Common Issues to Watch For

  • Driver Path Incorrect: Make sure the path to chromedriver.exe is valid and uses double backslashes (\\) on Windows.
  • Browser Compatibility: The version of ChromeDriver should match your installed version of Chrome.
  • Permissions: If you're on macOS or Linux, ensure the driver file has execution permissions.

Next Steps

Now that you’ve written your first test, here are a few ideas to build on:

  • Learn about different locators: ID, name, XPath, CSS selectors

  • Interact with elements: click(), sendKeys(), etc.

  • Use explicit waits to handle dynamic content

  • Organize tests using frameworks like JUnit or TestNG

  • Run tests in headless mode (without opening the browser window)

Final Thoughts

Writing your first Selenium script is a key milestone in learning test automation. With just a few lines of code, you can start interacting with web applications programmatically. As you get more comfortable, you’ll find powerful ways to automate complex user flows, integrate with CI/CD, and make your QA process faster and more reliable.

Would you like help adding more sections—like interacting with elements or using waits?

Here are some prerequisites for automation using Selenium, especially if you're getting started with writing test scripts:

1. Basic Knowledge Requirements

  • Programming Language Proficiency
    You must be comfortable with at least one programming language that Selenium supports:

    • Java (most commonly used)

    • Python

    • C#

    • JavaScript

    • Ruby

    • Kotlin

  • HTML and CSS
    Understand the basics of HTML structure and CSS selectors to locate and interact with web elements.

  • Basic JavaScript Understanding
    JavaScript knowledge helps when dealing with dynamic web elements or executing custom JS using Selenium.


2. Environment Setup

  • Install Java Development Kit (JDK) (if using Java)

  • Install an IDE like:

    • IntelliJ IDEA

    • Eclipse

    • VS Code (for Python/JavaScript)

  • Add Selenium Libraries to your project:

    • Download Selenium WebDriver libraries (JARs)

    • Use Maven/Gradle (for Java) to manage dependencies

  • Install Browser Drivers:

    • ChromeDriver for Google Chrome

    • GeckoDriver for Mozilla Firefox

    • EdgeDriver for Microsoft Edge


3. Testing Framework (Optional but Recommended)

To structure and run your tests efficiently:

  • Java: TestNG or JUnit

  • Python: PyTest or unittest

  • C#: NUnit or MSTest

  • JavaScript: Mocha or Jasmine


4. Web Browser

At least one browser installed for test execution:

  • Chrome

  • Firefox

  • Edge

  • Safari


5. Additional Tools & Concepts (Advanced)

  • Build Tools: Maven or Gradle (for Java)

  • Version Control: Git

  • CI/CD Tools: Jenkins, GitHub Actions, etc.

  • Understanding of Wait Mechanisms: Implicit, Explicit, Fluent waits

  • Knowledge of Locators: ID, Name, XPath, CSS Selector, ClassName, etc.

Comments

Popular posts from this blog

🚀 Applitools: The AI-Powered Visual Testing Tool Revolutionizing QA

In today’s fast-paced software development world, ensuring that your application looks perfect across devices and browsers is critical. This is where Applitools , a cutting-edge AI-based visual testing tool , steps in to make QA faster, smarter, and more efficient. 🔍 What is Applitools? Applitools is an AI-powered testing platform designed specifically for visual UI testing and monitoring . It uses Visual AI to detect any visual bugs in your application, making sure that what users see is exactly what you intended. With integrations across major test automation frameworks like Selenium, Cypress, Playwright, and Appium , Applitools offers cross-platform support with ease. 🧠 Why Use Applitools? 1. Visual AI for Precision Applitools doesn’t rely only on pixel-by-pixel comparison. Its Visual AI engine mimics human vision , identifying visual differences that actually matter and ignoring false positives. 2. Cross-Browser & Cross-Device Testing Run your test across 1000s of br...

Some trending tools.

As of 2025, Quality Assurance (QA) continues to evolve with a strong focus on automation, AI integration, and shift-left testing practices. Here are some of the top trending tools and technologies in QA: 🔧 Test Automation Tools Playwright Gaining popularity for end-to-end testing of web apps. Offers auto-waiting, headless execution, and multi-browser support. Good alternative to Selenium. Cypress Popular for front-end testing. Fast, developer-friendly, and great for CI/CD pipelines. TestCafe Node.js-based, easy to use for UI testing. Doesn’t require WebDriver like Selenium. Katalon Studio All-in-one solution for API, web, desktop, and mobile testing. Good for teams with less coding experience. Robot Framework Keyword-driven testing. Highly extensible and integrates well with Python libraries. 🤖 AI-Powered Testing Tools Testim (by Tricentis) Uses AI to stabilize and speed up UI tests. Reduces flakiness and test m...

AI tools and techniques that can be integrated into Selenium test cases to enhance automation with intelligence, self-healing, and smarter reporting.

  🔧 1. Healenium (Self-healing for Selenium) Use case : Automatically heals broken locators at runtime. Integration : Java-based and works directly with Selenium WebDriver. GitHub : https://github.com/healenium/healenium-web 📌 Benefits : Reduces test maintenance effort and improves test stability. 🧠 2. Testim (AI-based test execution and maintenance) Use case : Uses AI to identify elements and adapt to DOM changes. Integration : While Testim has its own framework, you can integrate via REST API or CI/CD pipelines. 📌 Alternative : Use AI features in the Testim platform for authoring and executing Java-based tests externally. 🔍 3. Applitools Eyes (Visual AI testing) Use case : Compares screenshots using AI to detect visual regressions. Integration : Java SDK available; easy to integrate with existing Selenium tests. Site : https://applitools.com 📌 Benefits : Detects UI bugs that are hard to find with DOM-based checks. 🗂️ 4. Mabl (AI ...