Use Python and Chrome to Take Webpage Screenshots
In 2017 Chrome, Google released a headless (no GUI) mode that can capture a screenshot of a web page from a specified viewport. This is useful for archiving pages for version comparison, monitoring, and client deliverables. Because it runs without a GUI, it is well suited for automation with Python. In just a few lines we’ll run headless Chrome, capture a screenshot, optimize it, and then use the result however you need. This workflow can help detect visual changes and surface performance opportunities. Let’s get started!
Table of Contents
Requirements and Assumptions
- Python 3 is installed and you understand basic Python syntax
- Access to a Linux installation (I recommend Ubuntu)
- Chrome installed
Starting the Script
First, install the optimize-images command-line tool to compress the screenshot into an optimized PNG file.
pip3 install optimize-images
Next, import the required standard modules. Outside core Python you’ll need time, datetime, and the os module to invoke the optimize-images executable. Remember that optimize-images is a command-line tool, so it must be run as an external program.
import os from datetime import datetime import time
Let’s set a few variables we’ll need. The name variable is used when creating the screenshot filename. Set name to your website name (no spaces). Set the url variable to the address of the page you want to capture.
name = "importsem"
url = "https://importsem.com"
getdate = datetime.now().strftime("%m-%d-%y")
Take the Screenshot
Use the os module to run Chrome in headless mode, hide scrollbars for a cleaner image, and set the window size. You may need to adjust the window size depending on your layout. Be sure to replace /PATH_TO_DESTINATION/ with your screenshot destination path. The script uses a 15-second delay to ensure the file is written before continuing. To compare original and optimized sizes, use os.stat() to read the file size.
try:
stream = os.popen("chromium-browser --headless --hide-scrollbars --screenshot='/PATH_TO_DESTINATION/" + name + "_org_" + getdate + ".png' --window-size=1920,1200 " + url)
time.sleep(15)
org_png = os.stat('/PATH_TO_DESTINATION/" + name + "_org_" + getdate + ".png').st_size
Optimize the Screenshot Image
Now that we have the PNG screenshot, optimize it to reduce file size. We’ll use the optimize-images tool. The documentation lists configuration options you can try for stronger compression; I leave the defaults and typically see about 10–15% savings. Again, update the destination path in the code. This step uses a 20-second delay to ensure the optimized file is ready before continuing.
stream = os.popen("optimize-images /PATH_TO_DESTINATION/" + name + "_op_" + getdate + ".png")
time.sleep(20)
op_png = os.stat('/PATH_TO_DESTINATION/" + name + "_op_" + getdate + ".png').st_size
Now close the try/except block and handle any errors.
except:
print("Screenshot failed")
Compare the Two Images
Finally, compare the file sizes and report the bytes saved by compression. First check that both images exist at the expected paths, then print each image size and the difference.
if os.path.isfile() == true and os.path.isfile() == true:
print("Original Image: " + org_png)
print("Optimized Image" + op_png)
print("Saved: " + org_png-op_png)
else:
print("One of the files doesn't exist")
Conclusion
You’ve learned how to capture screenshots from headless Chrome using Python. You can automate this script to store results in a database or loop through an entire site by loading a CSV from a Screaming Frog crawl containing every URL on your site. Try it out! Follow me on Twitter and share your Chrome screenshot ideas.
Python and Chrome Screenshot FAQ
How can I use Python and Chrome to take screenshots of webpages programmatically?
Use Selenium WebDriver to capture webpage screenshots. Write Python scripts that control Chrome via WebDriver, navigate to the desired page, and capture screenshots with the appropriate WebDriver commands.
What Python libraries are commonly used for taking screenshots with Chrome?
Selenium is a widely used Python library for browser automation, including taking screenshots. Together with ChromeDriver, it lets Python scripts interact with Chrome and capture page screenshots.
Are there specific dependencies or installations required to use Python and Chrome for webpage screenshots?
Yes. Install the Selenium library and download the ChromeDriver executable. Selenium installs via pip (pip install selenium), and you must download ChromeDriver and specify its path in your Python script.
Can I capture full-page screenshots using Python and Chrome?
Yes. Selenium combined with Chrome supports capturing full-page screenshots; use the appropriate commands to capture the entire page rather than only the visible viewport.
Are there alternatives to Selenium for taking screenshots with Python and Chrome?
While Selenium is common, other tools and libraries exist for controlling headless Chrome, such as Puppeteer or Pyppeteer. Selenium remains popular because of its versatility.
- Evaluate Subreddit Posts in Bulk Using GPT4 Prompting - December 12, 2024
- Calculate Similarity Between Article Elements Using spaCy - November 13, 2024
- Audit URLs for SEO Using ahrefs Backlink API Data - November 11, 2024





