How to connect to Chromium Headless using Selenium

我们两清 提交于 2019-11-29 01:42:46

I think the readme is a litte bit misleading. You don't have to start chromium itself and you can use the RemoteWebDriver. Make sure that 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 chromedriver to use chromium instead of chrome
  • Add "--headless" as 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 ChromeOptions
chromeOptions.addArguments("--headless")
new ChromeDriver(chromeOptions)

if you are using selenium 3+ chrome driver , you can simply use chrome options and initiate driver. Check details in a project

Example Project on Chrome Headless running with different options

 options.setHeadless(true)

*Use the following code:

ChromeOptions options = new ChromeOptions();  
options.setHeadless(true); //Set Chrome option
driver = 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 ChromeOptions

public 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 


    }

Chrome 59 has the ability to create instance as headless . I have tried for Windows with new chrome driver 2.30 and it worked for me https://www.automation99.com/2017/07/how-to-use-chrome-headless-using.html?m=1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!