简体   繁体   中英

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

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

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

或者你可以看看PhantomJS项目,这是一个无头的WebKit实现。

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 &

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

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.

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);

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.

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 (eg, 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 .

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM