Getting started with Headless Chrome on Windows

“A broken mannequin laying on the street in Taguatinga” by Edu Lauton on Unsplash

When it first became a thing, Headless Chrome wasn’t even available for Windows. You could only go headless on Linux with Chrome 59.

And Linux had superior headless browsing before! Virtual frame buffers.

lot of people still don’t realize you can run Chrome headless on Windows now. Well, you can, and it’s as easy as doing this from Powershell…

cd 'C:\Program Files (x86)\Google\Chrome\Application'

# Dump DOM to the screen
.\chrome.exe --headless --disable-gpu --enable-logging --dump-dom

# Save the page as a PDF
.\chrome.exe --headless --disable-gpu --print-to-pdf=C:\Temp\output.pdf

# Screenshot the page
.\chrome.exe --headless --disable-gpu --screenshot=C:\Temp\screenshot.png


Now what about automating Headless Chrome with Python and Selenium? Getting started there isn’t much harder.

Optional first step is to set up a virtual environment.

Second step is pip install selenium from your prompt.

Third step is download the chromedriver binary (≥ v2.32) into the directory where you’ll be writing your script.

And then your script can be as simple as…

from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
driver = webdriver.Chrome(chrome_options = chrome_options)

Check out these code samples on Github. If you’re up to it, fork the repo and do a pull request with your own experiments.

Like what you read? Give Randy Gingeleski a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.