Skip to main content

Command Palette

Search for a command to run...

First Selenium Program: Launching a Browser using Java

Updated
2 min read
First Selenium Program: Launching a Browser using Java
P

#pratiks-desk | I'm Pratik Joshi a B.Tech graduate in Computer Science and Engineering.

📌 Introduction

Selenium is a popular open-source tool for automating web browsers. If you're just getting started in automation testing or want to understand how browser automation works using Java, this post is for you.

In this quick guide, we’ll walk through a simple Selenium program that opens a browser — your first step toward automated testing.


🧰 Prerequisites

Before writing your first script, make sure you have:

  • Java JDK installed (Java 8 or later)

  • Eclipse or any Java IDE

  • Selenium WebDriver JAR files added to your project

  • ✅ A browser driver like ChromeDriver
    (📝 From Selenium 4+, the driver is handled automatically, so downloading or adding it to system path is usually not necessary unless you're using a custom setup)


👨‍💻 Code: Launching Chrome Browser

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

public class LaunchBrowser {
    public static void main(String[] args) {
        // Directly create ChromeDriver instance (Selenium 4+ handles driver management)

        // Set the path to the chromedriver executable, but from Selenium 4+ it's handled automatically,
        // so setting System.setProperty is no longer necessary unless you're using a custom setup.
        // System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe");

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

        // Open a website
        driver.get("https://www.google.com");

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

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

📌 Explanation

  • System.setProperty(...): Tells Selenium where your browser driver is located.

  • new ChromeDriver(): Launches the Chrome browser.

  • driver.get(...): Opens the desired URL.

  • driver.quit(): Closes all browser windows opened during the session.


🤔 Why Start with Browser Launching?

Launching the browser is the "Hello World!" of Selenium automation. It proves your setup is correct and helps you move on to test case automation, element interactions, and full end-to-end testing.


🎯 Next Steps

Now that you can launch a browser:

  • Learn to locate and interact with web elements (click, sendKeys, etc.)

Stay Tuned..!

Selenium with Java – From Zero to Test Hero

Part 1 of 1

"Selenium with Java – From Zero to Test Hero" is a beginner-friendly series that teaches Selenium WebDriver using Java -> from launching your first browser to writing real test scripts with clean code, clear tips, and no prior experience needed.