How to connect to Chromium Headless using Selenium?

SeleniumAutomation TestingTesting Tools

We can connect to Chromium headless with Selenium. The headless execution helps in reducing resource utilization and is a modern technique used in the industry.

Chrome can be used in headless mode after the 59 version. The ChromeOptions class is used to change the default browser behavior. The headless value is passed to the addArguments method as a parameter for headless execution.

Syntax

ChromeOptions op = new ChromeOptions();
op.addArguments("headless");
WebDriver d = new ChromeDriver(op);

Example

Code Implementation.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.util.concurrent.TimeUnit;
public class HeadlessChromium{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver", "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe");
      //ChromeOptions object
      ChromeOptions op = new ChromeOptions();
      //headless argument
      op.addArguments("headless");
      // configure options parameter to Chrome driver
      WebDriver driver = new ChromeDriver(op);
      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());
   }
}

Output

raja
Published on 28-Dec-2020 11:21:16
Advertisements