How to start ChromeDriver in headless mode?

SeleniumAutomation TestingTesting Tools

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

raja
Published on 30-Jan-2021 12:59:41
Advertisements