How to connect to Chromium Headless using Selenium How to connect to Chromium Headless using Selenium selenium selenium

How to connect to Chromium Headless using Selenium


I think the readme is a little bit misleading. You don't have to start Chromium itself and you can use the RemoteWebDriver. Make sure that a chromedriver is installed (https://sites.google.com/a/chromium.org/chromedriver/home).

  • Start chromedriver (e.g. ./chromedriver or ./chromedriver --port=9515)
  • Then you have tell the chromedriver to use Chromium instead of Chrome
  • Add --headless as an additional argument

Code should look like this:

final ChromeOptions chromeOptions = new ChromeOptions();chromeOptions.setBinary("/usr/bin/chromium-browser");chromeOptions.addArguments("--headless");desiredCapabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);WebDriver driver = new RemoteWebDriver(url, desiredCapabilities);

Worked for me on Ubuntu Linux.


Alternatively if your running it locally you can just do it like this. In scala.

val chromeOptions = new ChromeOptionschromeOptions.addArguments("--headless")new ChromeDriver(chromeOptions)


*Use the following code:

ChromeOptions options = new ChromeOptions();  options.setHeadless(true); //Set Chrome optiondriver = new ChromeDriver(options);  

and you will get "Headless" Chrome!

*Full code

import org.openqa.selenium.WebDriver;import org.openqa.selenium.chrome.ChromeDriver;import org.openqa.selenium.chrome.ChromeOptions;  //import ChromeOptionspublic class web_crawl {    private static WebDriver driver = null;    public static void main(String[] args) {       ChromeOptions options = new ChromeOptions();       options.setHeadless(true);       driver = new ChromeDriver(options);          driver.get("http://www.google.com");   //The website you want to connect to     }