Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

How to launch Chrome browser as headless in Selenium Java

Posted on 03/21/2025 By admin

“Headless” browser is a hot term now especially in executing UI automated scripts. When we launch a browser it opens up in a window or with a UI. We can launch a browser without that window or UI that is called a headless browser. A headless browser gives you a real browser context without the memory overhead of running a full version of a browser. All major browsers like Chrome, Firefox, Microsoft Edge support headless mode.

Running automated UI scripts in a headless browser may lower test execution time but not that much significantly. You can experience a little better performance during automated UI scripts execution. But using a headless browser we can minimize the chances of encountering system configuration issues like memory, the abrupt closure of the browser, etc. Execution of scripts in a headless browser is helpful when you don’t have any grid set up as it allows you to perform other tasks with scripts executions.

Earlier we had HtmlUnit Driver, PhantomJS (Suspended) for headless execution but they were not stable much. Capturing screenshot was a challenge and many did not support that.

In this post, we will learn to launch a Chrome browser in headless mode and will capture a screenshot using Selenium WebDriver Java.

There are two ways of launching a Chrome browser in headless mode:-

  1. Using setHeadless() method of ChromeOption class
  2. Using addArguments() method of ChromeOption class

Whatever way you use make sure you set window size as well. It helps in the smooth running of scripts, better resolution, and screen capturing.

package HeadlessBrowsers;


import java.io.File;
import java.io.IOException;

import org.openqa.selenium.OutputType;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import com.google.common.io.Files;

public class HeadlessChrome {
        
        public static void main(String[] args) throws IOException {
                
                // Set chrome executable path
                System.setProperty("webdriver.chrome.driver", System.getProperty("user.home")+"\\Downloads\\chromedriver_win32 (4)\\chromedriver.exe");
                
                // Set headless chrome
                ChromeOptions option=new ChromeOptions();
                //option.setHeadless(true);
                // OR - No need to add '--'
                option.addArguments("headless");
                // You should set window size for better resolution and screen capture
                option.addArguments("window-size=1200x600");
                
                ChromeDriver browser = new ChromeDriver(option);
                browser.get("https://chromedriver.chromium.org/downloads");
                
                // Capturing screenshot
                File file = browser.getScreenshotAs(OutputType.FILE);
                Files.copy(file, new File(System.getProperty("user.home")+"\\Downloads\\chromedriver_win32 (4)\\ss.png"));
                
                browser.close();
                
                
        }

}

You can see the captured screenshot by navigating to the path provided.

You can download/clone the above sample project from here.

If you have any doubt, feel free to comment below.If you like my posts, please like, comment, share and subscribe.#ThanksForReading

#HappyLearning

Uncategorized

Post navigation

Previous Post: Advanced Selenium Concepts – Custom Select Class – Select Value Containing Specific Text in Drop down
Next Post: REST Assured Tutorial 14 –RequestSpecification – How the request will look like

Related Posts

image – Make Selenium Easy Uncategorized
googletext – Make Selenium Easy Uncategorized
Handling Website Popups In Selenium webdriver | Make Selenium Easy Uncategorized
TestNgInLeftSide – Make Selenium Easy Uncategorized
Manual Testing | Make Selenium Easy Uncategorized
REST Assured Tutorial 29 – How to create POJO classes of a JSON Object Payload Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark