brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: chrome --headless --dump-dom prints the HTML after the JavaScript has run

Chrome
~/tips/tip-chrome-headless-dump-dom

google-chrome --headless --dump-dom https://example.com

Prints the page's HTML as it looks after JavaScript has run.

curl shows me what the server sent, and for a page that builds itself in the browser that is nearly empty. I would like to see the HTML the user sees, and Chrome can print it.

The headless Chrome post lists --dump-dom, which prints the DOM of the page to stdout:

google-chrome --headless --dump-dom https://example.com

The result is the document as it exists after the page loaded, so content that JavaScript added is in it. You can pipe it into the tools you already use:

google-chrome --headless --dump-dom https://example.com | grep -i "<title>"

Compare it with curl -s https://example.com on a client-rendered page. The curl version shows the empty shell and the Chrome version shows the rendered markup, which is the quickest way I know to find out whether a crawler without JavaScript would see anything.

The binary name depends on the system. The Chrome docs use google-chrome on Linux, so replace it with whatever your install is called.

Also, the post is marked deprecated and describes the old headless mode. Chrome 112 changed headless to the new mode and, since Chrome 132, the old one is only available as the separate chrome-headless-shell binary. Behaviour can differ between the two, so run the command once on your own version. The dump is a snapshot after load, so content that appears after a click or a long delay may be missing.

Further reading: Chrome: Headless mode, Getting started with headless Chrome (deprecated post)

More Chrome tips chrome://net-export/ google-chrome --user-data-dir=/tmp/chrome-clean --no-first-run Windows and Linux: Ctrl+Shift+A Settings > Search engine > Manage search engines and site search > Site search > Add chrome://net-internals/#hsts All Chrome tips (12) All topics

Link