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.
*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 }