blob: 739a1ad522b025c3a60c3d6fc794948974d40054 [file] [log] [blame] [view]
skyostilfe116162016-02-26 20:53:331# Headless Chromium
2
skyostil4c9d164c2016-12-23 11:15:523Headless Chromium allows running Chromium in a headless/server environment.
4Expected use cases include loading web pages, extracting metadata (e.g., the
5DOM) and generating bitmaps from page contents -- using all the modern web
6platform features provided by Chromium and Blink.
skyostilfe116162016-02-26 20:53:337
Mathias Bynens31eceb22023-08-17 04:52:418As of M118, precompiled `headless_shell` binaries are available for download
9under the name `chrome-headless-shell` via [Chrome for Testing
10infrastructure](https://googlechromelabs.github.io/chrome-for-testing/).
skyostilfe116162016-02-26 20:53:3311
Nikolay Vitkov5a48c6242025-01-20 08:58:3112As of M132, headless shell functionality is no longer part of
13the Chrome binary, so --headless=old has no effect.
14If you are using old Headless functionality you should
15now migrate to `chrome-headless-shell`.
16[Read more](https://developer.chrome.com/blog/removing-headless-old-from-chrome).
17
Peter Kvitek0cf1c8e02024-10-01 23:41:2318There are two ways to use Headless Chromium:
skyostilfe116162016-02-26 20:53:3319
Peter Kvitek0cf1c8e02024-10-01 23:41:2320## Usage via the DevTools remote debugging protocol
skyostilfe116162016-02-26 20:53:3321
Nikolay Vitkov5a48c6242025-01-20 08:58:31221. Start Chrome in headless mode using the `--headless` command line flag:
skyostilfe116162016-02-26 20:53:3323
Peter Kvitek0cf1c8e02024-10-01 23:41:2324```sh
Nikolay Vitkov5a48c6242025-01-20 08:58:3125$ chrome --headless --remote-debugging-port=9222 https://chromium.org/
Peter Kvitek0cf1c8e02024-10-01 23:41:2326```
skyostilfe116162016-02-26 20:53:3327
Peter Kvitek0cf1c8e02024-10-01 23:41:23282. Navigate to `chrome://inspect/` in another instance of Chrome.
skyostilfe116162016-02-26 20:53:3329
Peter Kvitek0cf1c8e02024-10-01 23:41:2330## Usage from Node.js
skyostilfe116162016-02-26 20:53:3331
Peter Kvitek0cf1c8e02024-10-01 23:41:2332For example, the [chrome-remote-interface](https://github.com/cyrus-and/chrome-remote-interface) Node.js package can be used to
33extract a page's DOM like this:
34
35```js
36const CDP = require('chrome-remote-interface');
37
38(async () => {
39 let client;
40 try {
41 // Connect to browser
42 client = await CDP();
43
44 // Extract used DevTools domains.
45 const {Page, Runtime} = client;
46
47 // Enable events on domains we are interested in.
48 await Page.enable();
49 await Page.navigate({url: 'https://example.com'});
50 await Page.loadEventFired();
51
52 // Evaluate outerHTML after page has loaded.
53 const expression = {expression: 'document.body.outerHTML'};
54 const { result } = await Runtime.evaluate(expression);
55 console.log(result.value);
56
57 } catch (err) {
58 console.error('Cannot connect to browser:', err);
59
60 } finally {
61 if (client) {
62 await client.close();
63 }
64 }
65})();
66```
67
68Alternatvely, the [Puppeteer](https://pptr.dev/guides/what-is-puppeteer) Node.js package can be used to communicate
69with headless, for example:
70```js
71import puppeteer from 'puppeteer';
72
73(async () => {
74 const browser = await puppeteer.launch({headless: 'shell'});
75
76 const page = await browser.newPage();
77 await page.goto('https://example.com');
78
79 const title = await page.evaluate(() => document.title);
80 console.log(title);
81
82 await browser.close();
83})();
84```
skyostil6db3da672016-10-11 02:52:1985
skyostil4c9d164c2016-12-23 11:15:5286## Resources and Documentation
87
88Mailing list: [[email protected]](https://groups.google.com/a/chromium.org/forum/#!forum/headless-dev)
skyostil120e5422017-02-21 13:21:5589
Sami Kyostilaa80b77b2017-05-15 15:20:2990Bug tracker: [Internals>Headless](https://bugs.chromium.org/p/chromium/issues/list?can=2&q=component%3AInternals%3EHeadless)
skyostil6db3da672016-10-11 02:52:1991
Sami Kyostilaa80b77b2017-05-15 15:20:2992[File a new bug](https://bugs.chromium.org/p/chromium/issues/entry?components=Internals%3EHeadless) ([bit.ly/2pP6SBb](https://bit.ly/2pP6SBb))
skyostil120e5422017-02-21 13:21:5593
Sami Kyostila40e4469f2017-05-26 16:06:5794* [Runtime headless mode on Windows OS](https://docs.google.com/document/d/12c3bSEbmpeGevuyFHcvEKw9br6CkFJSS2saQynBjIzE)
skyostilffe5b072017-02-28 17:03:3595* [BeginFrame sequence numbers + acknowledgements](https://docs.google.com/document/d/1nxaunQ0cYWxhtS6Zzfwa99nae74F7gxanbuT5JRpI6Y/edit#)
96* [Deterministic page loading for Blink](https://docs.google.com/document/d/19s2g4fPP9p9qmMZvwPX8uDGbb-39rgR9k56B4B-ueG8/edit#)
97* [Crash dumps for Headless Chrome](https://docs.google.com/document/d/1l6AGOOBLk99PaAKoZQW_DVhM8FQ6Fut27lD938CRbTM/edit)
skyostilb354f882016-12-13 18:42:4598* [Runtime headless mode for Chrome](https://docs.google.com/document/d/1aIJUzQr3eougZQp90bp4mqGr5gY6hdUice8UPa-Ys90/edit#)
Eric Seckler3a991f12017-12-05 15:21:0599* [Virtual Time in
100 Blink](https://docs.google.com/document/d/1y9KDT_ZEzT7pBeY6uzVt1dgKlwc1OB_vY4NZO1zBQmo/edit?usp=sharing)
Tiago Vignattif205adf2023-03-30 13:23:19101* [Headless Chrome architecture (Design Doc)](https://docs.google.com/document/d/11zIkKkLBocofGgoTeeyibB2TZ_k7nR78v7kNelCatUE)
skyostil6db3da672016-10-11 02:52:19102* [Session isolation in Headless Chrome](https://docs.google.com/document/d/1XAKvrxtSEoe65vNghSWC5S3kJ--z2Zpt2UWW1Fi8GiM/edit)
103* [Headless Chrome mojo service](https://docs.google.com/document/d/1Fr6_DJH6OK9rG3-ibMvRPTNnHsAXPk0VzxxiuJDSK3M/edit#heading=h.qh0udvlk963d)
104* [Controlling BeginFrame through DevTools](https://docs.google.com/document/d/1LVMYDkfjrrX9PNkrD8pJH5-Np_XUTQHIuJ8IEOirQH4/edit?ts=57d96dbd#heading=h.ndv831lc9uf0)
105* [Viewport bounds and scale for screenshots](https://docs.google.com/document/d/1VTcYz4q_x0f1O5IVrvRX4u1DVd_K34IVUl1VULLTCWw/edit#heading=h.ndv831lc9uf0)
106* [BlinkOn 6 presentation slides](https://docs.google.com/presentation/d/1gqK9F4lGAY3TZudAtdcxzMQNEE7PcuQrGu83No3l0lw/edit#slide=id.p)