How to start ChromeDriver in headless mode?


We can start chromedriver in headless mode. Headless execution is getting popular now−a−days since the resource consumption is less and execution is done at a faster speed.

Post version 59, Chrome supports headless execution. ChromeOptions class is utilized to modify the default characteristics of the browser. The addArguments method of the ChromeOptions class is used for headless execution and headless is passed as a parameter to that method.

Syntax

ChromeOptions opt = new ChromeOptions();
opt.addArguments("headless");
WebDriver drv = new ChromeDriver(opt);

Example

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.util.concurrent.TimeUnit;
public class HeadlessChrome{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver",
         "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      //ChromeOptions object
      ChromeOptions opt = new ChromeOptions();
      //headless argument
      opt.addArguments("headless");
      // configure options parameter to ChromeDriver
      WebDriver driver = new ChromeDriver(opt);
      driver.manage().timeouts().implicitlyWait(8, TimeUnit.SECONDS);
      driver.get("https://www.tutorialspoint.com/about/about_careers.htm");
      // get page title
      System.out.println("Page title in headless mode: " + driver.getTitle());
      driver.quit();
   }
}

Output

Updated on: 30-Jan-2021

993 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements