Getting started with Headless Chrome on Windows
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.
A 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 https://www.google.com/
# Save the page as a PDF
.\chrome.exe --headless --disable-gpu --print-to-pdf=C:\Temp\output.pdf https://www.google.com/
# Screenshot the page
.\chrome.exe --headless --disable-gpu --screenshot=C:\Temp\screenshot.png https://www.google.com/
W O W
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()
chrome_options.add_argument('headless')
driver = webdriver.Chrome(chrome_options = chrome_options)
driver.get('https://github.com/gingeleski')
print(driver.page_source)
driver.quit()
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.
Randy Gingeleski - GitHub - gingeleski.com - LinkedIn