Headless Browser Testing with Selenium using Java (original) (raw)

Last Updated : 18 May, 2026

Headless browser testing in Selenium using Java runs automation scripts without opening a visible browser window. It is mainly used for faster execution and efficient test runs in background environments such as CI/CD pipelines.

Selenium Headless Browser Testing in Java – Step by Step

Headless browser testing in Selenium allows automated test execution without opening the browser UI, making testing faster and more resource-efficient.

Step 1: Setting up the Selenium in Java

First of all, download the Selenium WebDriver JARs from the official Selenium website.Now, open Eclipse, create a new Java project, and add the downloaded WebDriver JAR to the project's build path.

Step 2: Installing the Necessary Modules

In Selenium WebDriver for Java, the required classes and interfaces are already available in the Selenium JAR files. You just need to import them into your program.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.WebElement;

Step 3: Defining Headless Browser Testing

In this step, we configure ChromeDriver options to modify the default behavior of the browser and enable headless mode, where the browser runs in the background without a visible UI.

// Import selenium libraries import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.WebElement;

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

    // Set ChromeDriver path
    System.setProperty("webdriver.chrome.driver",
            "C:\\Users\\Vinayak Rai\\Downloads\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe");

    // Define ChromeDriver options
    ChromeOptions options = new ChromeOptions();

    // Enable headless mode
    options.addArguments("--headless");

    // Launch browser in headless mode
    WebDriver driver = new ChromeDriver(options);
}

}

`

Step 4: Initiating the Web Driver and Navigate to the Webpage

In this step, we will define the path of the ChromeDriver and initiate the ChromeDriver using the **ChromeDriver function with **options as an argument, that defines headless browser testing. Further, we have opened the Geeks For Geeks website (**link) using the get() function.

// Import selenium libraries import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.WebElement;

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

    // Set ChromeDriver path
    System.setProperty("webdriver.chrome.driver",
            "C:\\Users\\Vinayak Rai\\Downloads\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe");

    // Define Chrome options
    ChromeOptions options = new ChromeOptions();

    // Enable headless mode
    options.addArguments("--headless");

    // Initialize WebDriver with options
    WebDriver driver = new ChromeDriver(options);

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

    // Close browser
    driver.quit();
}

}

`

Step 5: Finding an Element

In this step, we locate a web element using Selenium’s findElement() method. The element is identified using By.linkText(), which matches the visible text of a hyperlink on the webpage. Selenium returns a WebElement, which can then be used to perform actions like click, send keys, or retrieve text.

// Import selenium libraries import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.WebElement;

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

    // Set ChromeDriver path
    System.setProperty("webdriver.chrome.driver",
            "C:\\Users\\Vinayak Rai\\Downloads\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe");

    // Define Chrome options
    ChromeOptions options = new ChromeOptions();

    // Enable headless mode
    options.addArguments("--headless");

    // Optional: set window size for headless mode
    options.addArguments("window-size=1920,1080");

    // Initialize WebDriver
    WebDriver driver = new ChromeDriver(options);

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

    // Find element using link text
    WebElement link_text = driver.findElement(
            By.linkText("Data Structures and Algorithms")
    );

    // Click the link (optional but recommended)
    link_text.click();

    // Close browser
    driver.quit();
}

}

`

Step 6: Click on an Element

In this step, we perform a click action on a web element using Selenium’s click() method. Since the browser runs in headless mode, we cannot visually verify the action, so we print the page title using getTitle() to confirm navigation to the next page.

// Import selenium libraries import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.WebElement;

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

    // Set ChromeDriver path
    System.setProperty("webdriver.chrome.driver",
            "C:\\Users\\Vinayak Rai\\Downloads\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe");

    // Chrome options
    ChromeOptions options = new ChromeOptions();

    // Enable headless mode
    options.addArguments("--headless");

    // Set window size for headless mode
    options.addArguments("window-size=1920,1080");

    // Initialize driver
    WebDriver driver = new ChromeDriver(options);

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

    // Find element
    WebElement link_text = driver.findElement(
            By.linkText("Data Structures and Algorithms")
    );

    // Click element
    link_text.click();

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

    // Close browser
    driver.quit();
}

}

`

**Output: Learn Data Structures and Algorithms | DSA Tutorial - GeeksforGeeks

Advantages Headless Browser Testing

Headless browser testing improves speed, efficiency, and scalability in automated testing.