17

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:

geri-m
  • 665
  • 2
  • 11
  • 23
  • Did you get this to work? I did at one stage, similar to what you have done (I also used `RemoteWebDriver`), but I've come back to my setup a month later and it's not connecting.. – Clint Apr 08 '17 at 00:05
  • To get things running, we switched to phantomJS. By using Selenium's PhantomJSDriver and the WebWire Protocol we were able to use this setup. (Un)fortunatly, we have to investigate again, as the phantomJS maintainer is stepping down/Chrome 59 supports headless. (https://groups.google.com/forum/#!topic/phantomjs/9aI5d-LDuNE) – geri-m Apr 13 '17 at 10:59

7 Answers7

18

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.

Yuri
  • 4,254
  • 1
  • 29
  • 46
hkq
  • 196
  • 2
  • 5
  • 1
    This worked for me too, thanks! Now to fix the `an X display is required for keycode conversions, consider using Xvfb` exception! – Clint Apr 13 '17 at 21:31
  • For others: `$sudo apt-get install xvfb` `$xvfb-run /home/nikki/chromedriver --whitelisted-ips` – Clint Apr 13 '17 at 22:08
  • @hkq is Staring necessary? Because I ran my tests without start chrome driver and the tests ran headlessly – Rameshwar Nov 09 '17 at 17:34
  • @Rameshwar: No, it is not necessary but it is recommended. Otherwise you will start and terminate one process for each test. Taken from the chomedriver website: "The ChromeDriver class starts the ChromeDriver server process at creation and terminates it when quit is called. This can waste a significant amount of time for large test suites where a ChromeDriver instance is created per test." – hkq Dec 07 '17 at 16:00
  • `chromeOptions.setBinary(System.getProperty("chromeBinaryPath"));` helps me find the path – TomTomSamSam May 31 '19 at 19:27
5

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)
Stephen
  • 4,228
  • 4
  • 29
  • 40
4

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 
     
 
    }
kuo chang
  • 129
  • 4
1

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)
Shantonu
  • 1,280
  • 13
  • 12
0
System.setProperty("webdriver.chrome.driver", "Path of the chrome driver");
ChromeOptions chromeOptions = new ChromeOptions();  
chromeOptions.addArguments("--headless");
chromeOptions.addArguments("--window-size=1920,1200");
webDriver = new ChromeDriver(chromeOptions);

The invisible browser window is only 800x600 in size. Therefore, you need to set the desired screen size with an additional argument

Jwala Kumar
  • 525
  • 7
  • 9
0

for me works above solution:

chromeOptions.setBinary("/usr/bin/chromium-browser");

but i had to add (becouse of devtools):

chromeOptions.addArguments("--remote-debugging-port=9222");

and disable firewall

Pawel W
  • 101
  • 1
  • 1
  • 12
-2

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