Selenium with Java Tutorial (original) (raw)

Last Updated : 23 May, 2026

Selenium with Java is a tool for automating web application testing across different browsers and platforms. It helps testers build reliable, maintainable and scalable test automation suites.

Selenium Automation Workflow

Selenium Automation Workflow describes how Selenium WebDriver communicates between the test script and the web browser to perform automated actions such as clicking, typing, and navigation. It follows a step-by-step process using WebDriver APIs and browser drivers.

selenium_with_java_automation_workflow

Selenium Automation Workflow

Steps to Configure Java Environment

Step 1. Install JDK

First, configure the Java Development Kit on your system.

**Verify Installation:

java -version

Step 2. Install IDE – Eclipse, IntelliJ IDEA or NetBeans.

Now, install an Integrated Development Environment (IDE) such as Eclipse, IntelliJ IDEA or NetBeans.

Step 3. Download Selenium WebDriver

Step 4. Install Browser Driver

**Example Path:

D:\chromedriver_win32\chromedriver.exe

Selenium Manager in Selenium 4.6+

Modern Selenium versions (Selenium 4.6 and above) include Selenium Manager, which automatically downloads and manages browser drivers such as ChromeDriver and GeckoDriver. In many cases, manual driver setup and system property configuration are no longer required.

WebDriver driver = new ChromeDriver();

Selenium Manager automatically detects the browser version and configures the required driver during execution.

Create a Selenium with Java Project in Eclipse

**Step 1: Launch Eclipse and select.

**File -> New -> Java Project

Create Java Project

**Step 2: Enter a name for your project and click **Finish.

**Example: SeleniumJavaTutorial

Create Java Project

**Step 3: Right-click on your project in **Package Explorer and select **Properties.

Select Properties

**Step 4: Select Java Build Path from the left panel click on the libraries tab and then select Classpath. Click on Add External JARs and browse to the location where you downloaded the Selenium WebDriver library (e.g., **selenium-java-4.1.0.zip).

Add External JARs

**Step 5: Select all the JAR files inside the zip file and click Open and also all the files inside the lib folder.

**D:\selenium-java-4.11.0

**D:\selenium-java-4.11.0\lib

Select JAR files

**Step 6: Click Apply and Close to save the changes.

Save changes

**Step 7: Create a new package under your project by right-clicking on the src folder and selecting **New -> Package.

Create New Package

**Step 8: Add the name of your package

**Example: WebDriver

Add name of Package

**Step 9: Create a new class under your package (e.g., **WebDriver) by right-clicking on the package and selecting **New -> Class, then Enter a name for your class and click Finish.

Create New Class

**Step 10: After all the steps your file structure looks like this.

File Structure

Steps for Writing Code

**Step 1: Import the required packages at the top of your class:

Java `

import org.openqa.selenium.webdriver; import org.openqa.selenium.chrome.chromedriver;

`

After importing if still getting errors in import just delete the module-info.java file.

**Step 2: Create a main class inside the Web class.

Java `

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

`

**Step 3: Set the system property for ChromeDriver (path to chromedriver executable).

****(e.g., D:/chromedriver_win32/chromedriver.exe)**

Java `

System.setProperty("webdriver.chrome.driver", "D:/chromedriver_win32/chromedriver.exe");

`

**Step 4: Create an instance of ChromeDriver.

Java `

WebDriver driver = new ChromeDriver();

`

**Step 5: Navigate to the desired website.

driver.get("https://www.geeksforgeeks.org/");

Step 6: Get and print the page title.

Java `

String pageTitle = driver.getTitle(); System.out.println("Page Title: " + pageTitle);

`

**Step 7: Wait for a few seconds.

Java `

try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); }

`

**Step 8: Close the browser.

driver. Quit();

Below is the Java program to implement the above approach:

Java `

package webdriver;

import org.openqa.selenium.webdriver; import org.openqa.selenium.chrome.chromedriver;

public class Web { public static void main(String[] args) { // Set the system property for ChromeDriver (path to chromedriver executable) System.setProperty("webdriver.chrome.driver", "D:/chromedriver_win32/chromedriver.exe");

    // Create an instance of ChromeDriver (launch the Chrome browser)
    WebDriver driver = new ChromeDriver();

    try {
        // Navigate to the desired website (GeeksforGeeks in this example)
        driver.get("https://www.geeksforgeeks.org/");

        // Get and print the page title
        String pageTitle = driver.getTitle();
        System.out.println("Page Title: " + pageTitle);

        // Wait for a few seconds (for demonstration purposes only)
        Thread.sleep(3000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        // Close the browser
        driver.quit();
    }
}

}

`

**Output:
ggeks-selium

Handling Alerts, Frames and Windows

1. Alerts

import org.openqa.selenium.Alert;

// Switch to alert Alert alert = driver.switchTo().alert(); alert.accept(); // Click OK alert.dismiss(); // Click Cancel alert.getText(); // Get alert text

`

2. Frames

driver.switchTo().frame("frameName"); // By name/id driver.switchTo().frame(0); // By index driver.switchTo().defaultContent(); // Back to main page

`

3. Multiple Windows / Tabs

String parentWindow = driver.getWindowHandle(); for (String handle : driver.getWindowHandles()) { driver.switchTo().window(handle); } // Switch back to parent window driver.switchTo().window(parentWindow);

`