Master Headless Chrome: Run, Capture, and Export Pages from the CLI

Headless mode provides a GUI‑less browser environment that includes the full rendering engine and JavaScript engine, enabling developers to launch Chrome via the command line for tasks such as launching without UI, dumping the DOM, generating PDFs, and taking screenshots, useful for testing, automation, and scraping.

Node Underground
Node Underground
Node Underground
Master Headless Chrome: Run, Capture, and Export Pages from the CLI

Headless mode provides a GUI‑less browser environment that includes the full browser engine—JavaScript parser, rendering engine, etc.—but omits the visual UI, making it ideal for testing web applications such as automated screenshots, JavaScript testing, and data scraping.

In short, modern browsers are built for human users, while headless browsers are intended for programs.

Previously developers used third‑party headless browsers like PhantomJS; after Chrome introduced headless support, PhantomJS’s main maintainer Vitaly Slobodin announced he would cease development.

The article then lists common CLI scenarios for using headless mode.

Headless Chrome was released in Chrome 59. Currently, Chrome Canary is the only channel that includes Chrome 59, so to try headless Chrome you need to install Chrome Canary.

1. Launch Headless Mode

chrome --headless --remote-debugging-port=9222 https://chromium.org --disable-gpu

The --disable-gpu flag is required on Linux servers that lack a GPU; the option may be removed in future versions.

2. Print DOM Tree

chrome --headless --dump-dom https://www.chromestatus.com/

The --dump-dom flag prints the document.body.innerHTML content to the terminal.

3. Create PDF Document

chrome --headless --print-to-pdf https://www.chromestatus.com/

The --print-to-pdf flag outputs the page as a PDF file.

4. Capture Screenshot

chrome --headless --screenshot --window-size=1280,1696 https://www.chromestatus.com/

The --screenshot flag creates a file named screenshot.png in the current directory.

These features are just the tip of the iceberg; combining headless mode with Node.js unlocks many more possibilities.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

AutomationChromeweb testingheadless
Node Underground
Written by

Node Underground

No language is immortal—Node.js isn’t either—but thoughtful reflection is priceless. This underground community for Node.js enthusiasts was started by Taobao’s Front‑End Team (FED) to share our original insights and viewpoints from working with Node.js. Follow us. BTW, we’re hiring.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.