Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to start Google Chrome in headless mode?

I carefully revised the list of switches at http://peter.sh/experiments/chromium-command-line-switches/#chrome-frame and I couldn't find anything that would launch Chrome in a hidden background process.

The closest I was able to is --keep-alive-for-test + custom packaged app, but the app fails to execute any passed code because (the way it reports) "no window - ChromeHidden".

like image 881
Silviu-Marian Avatar asked Feb 09 '12 12:02

Silviu-Marian


People also ask

How do I run in headless mode?

You can run Google Chrome in headless mode simply by setting the headless property of the chromeOptions object to True. Or, you can use the add_argument() method of the chromeOptions object to add the –headless command-line argument to run Google Chrome in headless mode using the Selenium Chrome web driver.

Is it possible to run Google Chrome in headless mode with extensions?

You can run Chrome with extensions headless using Xvfb. Use chrome-remote-interface (or another Chrome Debug Protocol client) to trigger the screenshot.

What does it mean to run Chrome headless?

Headless Chrome is a way to run the Chrome browser in a headless environment without the full browser UI. One of the benefits of using Headless Chrome (as opposed to testing directly in Node) is that your JavaScript tests will be executed in the same environment as users of your site.


2 Answers

TL;DR

google-chrome --headless --remote-debugging-port=9222 http://example.com 

You'd also need --disable-gpu temporarily.


Tutorial:

https://developers.google.com/web/updates/2017/04/headless-chrome


There's a work in progress: https://code.google.com/p/chromium/issues/detail?id=546953

The main deliverables are:

  1. A library which headless applications can link to to.
  2. A sample application which demonstrates the use of headless APIs.

So it would be possible to create a simple application that runs in console without connecting to display.

Update Apr 18 '16: The work is mainly done. There's a public forum now:

https://groups.google.com/a/chromium.org/forum/#!forum/headless-dev

Documentation is being in progress:

https://chromium.googlesource.com/chromium/src/+/master/headless/README.md

Update Sep 20 '16: It looks like chrome will eventually get the "--headless" parameter: https://bugs.chromium.org/p/chromium/issues/detail?id=612904

There was a presentation on BlinkOn 6 (June 16/17, 2016)

Update Nov 29 '16: Design doc for --headless flag: https://docs.google.com/document/d/1aIJUzQr3eougZQp90bp4mqGr5gY6hdUice8UPa-Ys90/edit#heading=h.qxqfzv2lj12s

Update Dec 13 '16: --headless flag is expected to be available in Canary builds soon

Update Mar 12 '17: Chrome 57 has a --headless flag working. Waiting for Selenium and other tools to catch up. User guide: https://chromium.googlesource.com/chromium/src/+/lkgr/headless/README.md

like image 130
Vanuan Avatar answered Oct 06 '22 02:10

Vanuan


This guy managed to run Chrome headlessly by using Xvfb (X virtual frame buffer) to trick Chrome into thinking it was displaying a window:

http://e-method.blogspot.fr/2010/11/google-chrome-with-xvfb-headless-server.html

If you're on Linux you could try that.

So basically you need to install X virtual frame buffer and Google Chrome via:

root@localhost: ~# apt-get install xvfb imagemagick root@localhost: ~# apt-get install google-chrome 

Then run the browser on the display:

root@localhost: ~# xvfb-run --server-args='-screen 0, 1024x768x24' \ google-chrome -start-maximized http://www.example.com \ > & /dev/null & root@localhost: ~# DISPLAY=:99 import -window root myimage.png 
like image 41
Chris B Avatar answered Oct 06 '22 04:10

Chris B