How to connect to Chromium Headless using Selenium

I would like to use a chrome head for automatic testing using selenium. ( https://chromium.googlesource.com/chromium/src/+/lkgr/headless/README.md )

I have a headless version already running on 9222. So if I open http://10.252.100.33:9222/json/I you get

[ { "description": "", "devtoolsFrontendUrl": "/devtools/inspector.html?ws=127.0.0.1:9223/devtools/page/0261be06-1271-485b-bdff-48e443de7a91", "id": "0261be06-1271-485b-bdff-48e443de7a91", "title": "The Chromium Projects", "type": "page", "url": "https://www.chromium.org/", "webSocketDebuggerUrl": "ws://127.0.0.1:9223/devtools/page/0261be06-1271-485b-bdff-48e443de7a91" } ] 

As a next step, I would like to associate selenium with headless chrome. But when I try

 final DesiredCapabilities caps = DesiredCapabilities.chrome(); final WebDriver driver = new RemoteWebDriver(new URL("http://localhost:9222/json"), caps); driver.get("http://www.google.com"); 

I get the following logout

 Jän 24, 2017 7:14:45 PM org.openqa.selenium.remote.ProtocolHandshake createSession INFORMATION: Attempting bi-dialect session, assuming Postel Law holds true on the remote end Jän 24, 2017 7:14:45 PM org.openqa.selenium.remote.ProtocolHandshake createSession INFORMATION: Falling back to original OSS JSON Wire Protocol. Jän 24, 2017 7:14:45 PM org.openqa.selenium.remote.ProtocolHandshake createSession INFORMATION: Falling back to straight W3C remote end connection org.openqa.selenium.SessionNotCreatedException: Unable to create new remote session. desired capabilities = Capabilities [{browserName=chrome, version=, platform=ANY}], required capabilities = Capabilities [{}] Build info: version: '3.0.1', revision: '1969d75', time: '2016-10-18 09:49:13 -0700' System info: host: 'Geralds-MacBook-Pro.local', ip: '192.168.0.249', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.12.2', java.version: '1.8.0_111' Driver info: driver.version: RemoteWebDriver 

Questions:

+9
java selenium chromium headless
source share
5 answers

I think readme is a bit confusing. You do not need to run the chrome itself, and you can use RemoteWebDriver. Make sure the chrome recorder is installed ( https://sites.google.com/a/chromium.org/chromedriver/home ).

  • Launch chrome reverse (e.g. ./chromedriver or ./chromedriver --port=9515 )
  • Then you indicated that chromedriver uses chrome instead of chrome
  • Add "--headless" as an optional argument

The 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.

+12
source share

Alternatively, if you use it locally, you can simply do it like this. In scala.

 val chromeOptions = new ChromeOptions chromeOptions.addArguments("--headless") new ChromeDriver(chromeOptions) 
+4
source share

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

Sample project in Chrome Headless with various options

  options.setHeadless(true) 
+1
source share

* Use the following code:

 ChromeOptions options = new ChromeOptions(); options.setHeadless(true); driver = new ChromeDriver(options); 

and you get Chrome Headless!

* Full code

 import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.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 } 
0
source share

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

-2
source share

All Articles