62

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

Silviu-Marian
  • 10,565
  • 6
  • 50
  • 72

11 Answers11

66

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

Vanuan
  • 31,770
  • 10
  • 98
  • 102
  • 2
    Dam still not on Windows. Shouldn't Windows be the priority seeing that most people (even devs) are using Windows? – Pacerier Feb 22 '17 at 01:40
  • 3
    @Pacerier AFAIK, most web developers are using Unix-based systems. CI servers are mainly on Linux. Most users use Windows, it's true. But that's why people like web: it's platform independent, so browsers work the same on all platforms. – Vanuan Feb 24 '17 at 06:42
  • 1
    As of today, Chrome 59 includes the `--headless` option (Mac & Linux). Btw, would you know how to list all *chrome* command line options? – Déjà vu Jun 07 '17 at 01:01
  • previously I missed the option `--remote-debugging-port`, I found that the chromium process was not started, it worked by adding the option. – Jinlxz Liu Oct 27 '20 at 08:52
24

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
kenorb
  • 155,785
  • 88
  • 678
  • 743
Chris B
  • 9,149
  • 4
  • 32
  • 38
  • 3
    That link doesn't work anymore, but found this one by googling: http://e-method.blogspot.fr/2010/11/google-chrome-with-xvfb-headless-server.html – Ritesh Jan 07 '13 at 21:36
  • I used Vagrant/Ansible to configure headless Chrome on an Ubuntu VM: https://github.com/alexwoolford/vagrantHeadlessChrome I'm on a Mac, but believe this will work on a Windows box. – Alex Woolford Sep 06 '14 at 15:50
  • Chris B I tried this on Ubuntu [See HERE](http://askubuntu.com/questions/754382/how-do-i-start-chromium-browser-in-headless-mode-extension-randr-missing-on-d) but doesn't seem to work for me. Any further pointers? – zabumba Apr 07 '16 at 15:31
  • @GRIGORE-TURBODISEL you can run [Ubuntu on Windows](http://www.zdnet.com/article/how-to-run-linux-desktop-apps-on-windows/) now :) . – Vanuan Apr 18 '16 at 04:27
  • @Iordanis, @ Chris, But unlike the true headless Chrome, I don't think you get to call those remote control APIs and functions isn't it? How are you going to "drive" the browser then? – Pacerier Feb 22 '17 at 01:42
  • Hi, how would this work if trying to run cucumber tests? – Richlewis Apr 13 '17 at 10:58
12

Or you can look at PhantomJS project which is a headless WebKit implementation.

Anthony O.
  • 22,041
  • 18
  • 107
  • 163
  • 3
    It is, I know if for a fact because I already tested it. You can test pretty much any JS-capable solution out there against, for instance, Sencha's ExtJS4 complex demos, or Google's Adwords Keyword Tool interface. – Silviu-Marian May 21 '12 at 12:26
  • 4
    WebKit has been superseded by blink lately so PhantomJS no longer emulates modern Chrome versions. – matanster Dec 28 '13 at 11:07
  • PhantomJS doesn't support IndexedDb either – ocodo Sep 28 '14 at 06:13
  • PhantomJS doesn't support the Web Audio API, making it a no-go for me. – Brad Aug 17 '15 at 03:22
  • 2
    Phantom was 10 times slower on my machine, has been using Selenium/chromedriver since. – arkod Sep 24 '15 at 11:26
  • I am also looking towards headless chrome/chromium, as PhantomJS not only slower, but expensive on CPU tasks – dr.dimitru Feb 15 '16 at 12:07
  • Phantomjs decided to maintain its own Qt WebKit fork (Qt 5.6 uses chrome's engine), which makes its future moot. I.e. I don't believe they'd be able to keep up with Apple. So it'll always be somewhat old browser. It might be even better if you want to support older browsers. – Vanuan Apr 18 '16 at 04:05
  • **Chrome is more than just Webkit**. – Pacerier Feb 22 '17 at 01:42
  • We need to capture screenshots of embedded MP4 videos. Since phantomjs does not support the audio/video element. We cannot use this otherwise great tool – Chris Mar 21 '17 at 07:56
  • 1
    PhantomJS will yield to chrome headless: https://www.infoq.com/news/2017/04/Phantomjs-future-uncertain – eflat Jul 25 '17 at 21:57
7

You could set up a linux VM and use xvfb in it.

Installation on debian / ubuntu:

sudo aptitude install xvfb

Start Chrome headless and visit http://example.com :

xvfb-run --server-args='-screen 0, 1024x768x16' google-chrome 
    -start-maximized http://example.com > /dev/null &
djangofan
  • 28,471
  • 61
  • 196
  • 289
mit
  • 11,083
  • 11
  • 50
  • 74
7

Turns out it starts in headless mode if you start it as a child subprocess. Besides that:

  • nircmd.exe can do win hide on chrome based on its PID
  • Autohotkey_L can also start Chrome hidden without a taskbar button
avetisk
  • 11,651
  • 4
  • 24
  • 37
Silviu-Marian
  • 10,565
  • 6
  • 50
  • 72
  • 3
    I realise that's an old answer, but is it possible to point to any code sample showing this please? Thanks a lot. – Meligy Nov 01 '15 at 02:31
  • But you still need to run X server to start a parent process, right? Or do you mean to say that parent process isn't necessary to be chrome? – Vanuan Apr 18 '16 at 04:09
  • Heh, I've re-read the question. Windows can't be run headlessly :) – Vanuan Apr 18 '16 at 04:24
5

The Chromium Embedded Framework project seems like it might fit your usecase. I don't have personal experience with the project, but I've heard good things, and it has a solid API that you should be able to exploit for your purposes.

Mike West
  • 5,097
  • 25
  • 26
  • There's also Chromium Content Shell: https://github.com/adobe/brackets/wiki/CEF3-vs.-Chromium-Content-Shell – Vanuan Dec 26 '15 at 22:40
3

I don't have enough reputation to comment yet, but want to let you guys know that the chrome headless mode which Vanuan mentions actually works with Selenium webdriver.

In Java you can pass the flag to chrome through chromeDriver with the following code:

ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
ChromeDriver chromeDriver = new ChromeDriver(options);
74nine
  • 838
  • 7
  • 13
2

I've recently found this article which mentions several commandline options that seem to do it. Using these keywords I googled out this piece of code which seem to confirm that these options exist.

// Does not automatically open a browser window on startup (used when
// launching Chrome for the purpose of hosting background apps).
const char kNoStartupWindow[]               = "no-startup-window";

// Causes Chrome to launch without opening any windows by default. Useful if
// one wishes to use Chrome as an ash server.
const char kSilentLaunch[]                  = "silent-launch";

I managed to successfuly run Chrome with --no-startup-window and indeed it launched without any windows. It looked like it launched properly, it spawned all typical children, but the website I tried to make it load inside didn't seem to be actually visited. It maybe possible that this headless mode is only for running apps and not for visiting sites headless*), but it looks very promising as the normal worker tree is set up, just no windows.

The second option --silent-launch made chrome process very silent. I didn't notice any children spawned and the process exited promptly. I doubt it'll be usable for this case.

After I failed my attempts with these options, I focused on less sophisticated ways. On the bottom of the list there are two options:

// Specify the initial window position: --window-position=x,y
const char kWindowPosition[]                = "window-position";

// Specify the initial window size: --window-size=w,h
const char kWindowSize[]                    = "window-size";

I ran Chrome with options to move it completely out of the working area:

--window-size=800,600 --window-position=-800,0

and as dirty as it feels, sure it's no true headless, but still the window is out of my sight, and everything done just with chrome's startup options, without external tools sending low-level window-hide messages.

*) yes, I know try to do weird things. Essentially I tried to get rid of Chrome window that's kept by Karma during tests. I know I can switch to PhantomJS, but I specifically needed to run them in Chrome, and the window popping out was.. eh.. disturbing in the long run.

Brad
  • 159,648
  • 54
  • 349
  • 530
quetzalcoatl
  • 32,194
  • 8
  • 68
  • 107
0

It is currently under development, you can read more information about it from here: https://chromium.googlesource.com/chromium/src/+/master/headless/README.md

Headless Chromium is a library for running Chromium in a headless/server environment. Expected use cases include loading web pages, extracting metadata (e.g., the DOM) and generating bitmaps from page contents -- using all the modern web platform features provided by Chromium and Blink.

It currently works on Linux, there is a nice presentation.

Mohamed Mansour
  • 39,445
  • 10
  • 116
  • 90
0

I was also able to make chrome headless work with NightwatchJS. Here is the config that let me use it:

    "chromeHeadless": {
  "desiredCapabilities": {
    "browserName": "chrome",
    "chromeOptions": {
      "args": ["--headless"],
      "binary": "/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary"
    }
  }
}
timetraveler90
  • 336
  • 2
  • 16
0

Chrome 59 has the ability to create instance as headless. Find the below tutorial https://www.automation99.com/2017/07/how-to-use-chrome-headless-using.html?m=1