Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to Chromium Headless using Selenium

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

I do have the headless version already running on 9222. So if i open http://10.252.100.33:9222/json/I do 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'd like to connect selenium to the headless chromium. 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 do 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's 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 are:

  • Is the RemoteWebDriver the correct driver to connect to the headless chromium?
  • I read about the DevTool protocol (https://docs.google.com/presentation/d/1gqK9F4lGAY3TZudAtdcxzMQNEE7PcuQrGu83No3l0lw/), but I'm not sure, how to create such a client using selenium.
  • Connecting the Chromium Headless using the Chrome DevTools works (https://developers.google.com/web/tools/chrome-devtools/remote-debugging/) besides some segmentation vaults ;-)
like image 661
geri-m Avatar asked Jan 24 '17 18:01

geri-m


People also ask

How do I use Chrome headless in Selenium?

ChromeOptions options = new ChromeOptions() options. addArgument("headless"); ChromeDriver driver = new ChromeDriver(options); In the above code, the browser is instructed to run in the headless mode using the addArgument() method of the ChromeOptions class provided by the Selenium WebDriver.

Can you use Selenium with Chromium?

Through WebDriver, Selenium supports all major browsers on the market such as Chrome/Chromium, Firefox, Internet Explorer, Edge, and Safari. Where possible, WebDriver drives the browser using the browser's built-in support for automation.

Does Selenium support headless browser?

Headless testing is simply running your Selenium tests using a headless browser. It operates as your typical browser would, but without a user interface, making it excellent for automated testing.

How do I run Chrome headless?

Which command starts the google chrome web browser in headless mode? As we have already seen, you just have to add the flag –headless when you launch the browser to be in headless mode. With CLI (Command Line Interface), just write: chrome \<br> – headless \ # Runs Chrome in headless mode.


4 Answers

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.

like image 52
hkq Avatar answered Oct 24 '22 22:10

hkq


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)
like image 40
Stephen Avatar answered Oct 24 '22 23:10

Stephen


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 
     
 
    }
like image 25
kuo chang Avatar answered Oct 24 '22 22:10

kuo chang


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)
like image 26
Shantonu Avatar answered Oct 25 '22 00:10

Shantonu