Running and Controlling Headless Chrome via Command Line and Programmatic Interfaces
This article explains how to install Chrome, configure command‑line aliases, launch Headless Chrome with various flags, and interact with it both through direct CLI commands and programmatic Node.js libraries such as chrome‑launcher and chrome‑remote‑interface.
Building on the previous overview of Headless Chrome, this guide details how to run and control Headless Chrome using command‑line tools and programmable interfaces.
1. Running Headless Chrome from the Command Line
1.1 Chrome Installation (requires VPN)
Download URL: https://www.chromium.org/getting-involved/dev-channel
Version comparison: Chromium – open‑source base, hourly updates. Canary – daily build, highly experimental. Dev – weekly build for developers. Beta – monthly stable testing. Stable – roughly monthly stable releases.
On Windows, add standalone=1 to the download URL to obtain an offline installer.
1.2 Command‑Line Alias Configuration (macOS)
Add the following lines to ~/.bashrc :
alias chrome="/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome"
alias chrome-canary="/Applications/Google\ Chrome\ Canary.app/Contents/MacOS/Google\ Chrome\ Canary"After reopening the terminal, you can launch the stable Chrome with chrome and the Canary build with chrome-canary .
1.3 Launching Chrome with Flags
Standard launch: chrome https://www.baidu.com
Headless mode (Chrome ≥ 59): chrome --headless https://www.baidu.com
Print page to PDF: chrome --headless --print-to-pdf https://www.baidu.com (produces output.pdf )
Take a screenshot: chrome --headless --screenshot --window-size=414,736 https://www.baidu.com (produces screenshot.png )
Open REPL: chrome --headless --repl
Enable remote debugging: chrome --headless --remote-debugging-port=9222
Reference full flag list: Chromium command‑line switches
1.4 Operating Headless Chrome via CLI Tools
Start Headless Chrome with remote debugging: chrome --headless --remote-debugging-port=9222
Install the Node.js client: npm install chrome-remote-interface -g
Show usage of chrome-remote-interface (list, new, inspect, version, etc.).
Open a new page: chrome-remote-interface new https://www.baidu.com
Inspect the page: chrome-remote-interface inspect
Evaluate JavaScript in the page context: Runtime.evaluate({expression:'location.href'})
2. Programmatic Ways to Run Headless Chrome
2.1 Invoking Chrome via System Calls
The Lighthouse project provides a reusable chrome-launcher NPM component that abstracts cross‑platform launching. Example:
const chromeLauncher = require('chrome-launcher');
chromeLauncher.launch({
chromeFlags: ['--headless']
}).then(chrome => {
console.log(chrome);
chrome.kill();
});2.2 Using a Client Library for Chrome DevTools Protocol
The chrome-remote-interface library implements the Chrome DevTools Protocol for Node.js. Example that launches Chrome, connects to the debugging port, enables Network and Page domains, navigates to a URL, logs network requests, and shuts down after 5 seconds:
const chromeLauncher = require('chrome-launcher');
const CDP = require('chrome-remote-interface');
chromeLauncher.launch({port: 9222, chromeFlags:['--headless']}).then(launcher => {
CDP.Version({host:'localhost',port:9222}).then(versionInfo => console.log(versionInfo));
CDP({host:'localhost',port:9222}).then(chrome => {
const {Network, Page} = chrome;
Network.enable();
Page.enable();
Network.requestWillBeSent(params => console.log(params.request.url));
Page.navigate({url:'https://www.baidu.com'});
setTimeout(() => launcher.kill(), 5000);
});
});These examples demonstrate both low‑level command‑line control and high‑level programmatic interaction with Headless Chrome for automation, testing, and data extraction.
Qunar Tech Salon
Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.