First Selenium Program: Launching a Browser using Java


๐ 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..!
Subscribe to my newsletter
Read articles from Pratik J directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

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