How to take website screenshots in Python

Updated on Dmytro Krasun 8 min read
With Python, you can take website screenshots in multiple ways. But the best way to do it depends solely on your needs and your use case. Let's quickly examine all the options.

Quick Overview

Playwright is the best library to automate website screenshots in Python today—no doubts. It is easy to install and use.

But if you have a few minutes and want to examine all the options to take screenshots of web pages in Python, you can use the following:

  1. An old but battle-tested automation testing framework like Selenium with a web driver of choice.
  2. pyppeteer, although not maintained and outdated, is still a usable Python port of the JavaScript (headless) Chrome/Chromium browser automation library. It is also one of the ways to screenshot a webpage without Selenium.
  3. Playwright, a modern successor to both Selenium and pyppeteer, allows for browser automation and automated testing.
  4. You can use any URL to screenshot API to quickly start without managing browsers and access a variety of useful automation features out-of-the-box.

Let’s quickly give an example for each option and assess which one is better to use and when.

Selenium in Python

A quick tip: Use Selenium for rendering screenshots in Python if you already have that library in your stack. Otherwise, if the goal is to render to screenshots only, go with Playwright. Prefer Playwright to Selenium for testing also, since the former is actively developed and supports assertions and almost all modern browsers.

Selenium is a library for automating web browsers. It allows you to control a web browser, such as Google Chrome, Firefox, or Safari, from Python.

Selenium provides a way to write scripts in Python that can interact with web pages in a way that simulates a user interacting with the web page through a web browser. It is useful for a variety of tasks, such as testing web applications, automating boring and repetitive tasks, taking screenshots and web scraping.

Selenium requires a web driver to control a web browser through Python. Some popular web drivers include chromedriver for Google Chrome, geckodriver for Mozilla Firefox, and safaridriver for Safari.

Install Selenium and your driver of choice using pip:

Terminal window
pip install selenium
pip install chromedriver # or geckodriver, or safaridriver

And now let’s write a simple script to take a screenshot in Python:

from selenium import webdriver
# 1. create a web driver instance
driver = webdriver.Chrome()
# 2. navigate to the website
driver.get("https://magician.design/")
# 3. save a screenshot of the current page
driver.save_screenshot("magician.design.png")
# 4. close the web driver
driver.quit()

The result is:

A screenshot of Magician

Use Selenium if you want to take screenshots from a different browser, but keep in mind that its screenshot capabilities are minimal. If you are OK with using Chrome or Chromium and don’t care about other browsers but need a powerful library to take screenshots, check out pyppeteer.

pyppeteer

A quick tip: The pyppeteer library is outdated and not maintained anymore. For simple use cases you might still use it, but consider using Playwright instead.

pyppeteer is a Python port of the JavaScript (headless) Chrome/Chromium browser automation library called Puppeteer.

Install with pip from PyPI (pyppeteer requires Python >= 3.6):

pip install pyppeteer

Let’s open a webpsite and take a screenshot of it:

import asyncio
from pyppeteer import launch
async def main():
browser = await launch()
page = await browser.newPage()
await page.goto('https://softstart.app/')
await page.screenshot({'path': 'softstart.png'})
await browser.close()
asyncio.get_event_loop().run_until_complete(main())

The screenshot is stored at softstart.png. That’s it and that’s how simple it is:

A screenshot of Softstart

Playwright in Python

A quick tip

Playwright is specifically designed to meet the requirements of end-to-end testing.

Playwright offers support for all contemporary rendering engines, such as Chromium, WebKit, and Firefox.And it allows testing across various operating systems including Windows, Linux, and macOS, whether locally or on continuous integration (CI) systems, and supports both headless and headed modes with native mobile emulation.

It is a very powerful library and it can do mostly everything that a browser can do:

  • To crawl a single-page application or generate server-side rendered content.
  • To take screenshots and create PDF documents of web pages.
  • To automate tasks such as form submission, user interface testing, and keyboard input.
  • To set up an automated testing environment using current JavaScript and browser capabilities.
  • To record a timeline trace of your website to troubleshoot performance issues.
  • To automate testing of Chrome extensions.

But’s get back to the screenshots and check how easy it is to do it with Playwright.

First, install it:

Terminal window
pip install playwright
playwright install

Then let’s take a screenshot:

from playwright.sync_api import sync_playwright
def run(playwright):
# launch the browser
browser = playwright.chromium.launch()
# opens a new browser page
page = browser.new_page()
# navigate to the website
page.goto('https://example.com')
# take a full-page screenshot
page.screenshot(path='example.png', full_page=True)
# always close the browser
browser.close()
with sync_playwright() as playwright:
run(playwright)

That’s how simple it is:

The example.com website

If you want to screenshot webpages without Selenium, Playwright is your best choice.

You might encounter a few issues while using Playwright, let’s quickly consider how to solve them if you get to them.

URL to Screenshot API

One of the most significant downsides of taking screenshots with Selenium, pyppeteer or even Playwright is handling infrastructure and taking care of browsers. They are resource intensive. You might encounter crashes, memory leaks, and high levels of CPU consumption.

So, if you need to take 5-10 screenshots and not that often, I would go with Selenium, pyppeteer or Playwright. But if you need a scale or don’t want to deal with infrastructure issues, I would use a third-party API, like ScreenshotOne—an URL to screenshot API.

I am a founder of ScreenshotOne, and of course, I would love to everybody be my customers. But that’s not the goal. The goal is to serve the customers who can extract the maximum value from my product.

If you do have time to deal with screenshot issues and corner cases, I would recommend choosing Playwright and going with it. But if you want to offload that work to somebody else and focus on your core business priorities, I am happy to help.

It is free to get started and supports various options—like removing chat widgets, blocking cookie banners, rendering in dark mode, and even animations.

Sign up to get your API access and secret key, and let’s try ScreenshotOne’s official SDK for Python:

Terminal window
pip install screenshotone

And:

import shutil
from screenshotone import Client, TakeOptions
# create API client
client = Client('<your access key>', '<your secret key>')
# set up options
options = (TakeOptions.url('https://screenshotone.com')
.format("png")
.viewport_width(1024)
.viewport_height(768)
.block_cookie_banners(True)
.block_chats(True))
# or render a screenshot and download the image as stream
image = client.take(options)
# store the screenshot the example.png file
with open('screenshotone.png', 'wb') as result_file:
shutil.copyfileobj(image, result_file)

That’s it. The result is:

A screenshot of ScreenshotOne

That’s how simple it was!

If you have any questions, please, reach out to support@screenshotone.com and I would love help. I am so passionate about my niche that I will help you with screenshot automation even if you are not my customer and don’t plan to be.

Summary

I would recommend using Selenium only if you are already using it. But if you need screenshots using Python, the best options are to use Playwright or to use a screenshot API.

Every time, I would choose Playwright and prefer it to anything, except if you know that you need to take screenshots at scale and don’t want to deal with the corner cases. Then I would give a try to URL to screenshot API like ScreenshotOne

I hope I helped you a little bit to choose the best Python library to take screenshots and have a nice day!

Frequently Asked Questions

As a bonus material, I am gathering all the questions users often ask related to Python screenshotting. If you have any, also, please send me at support@screenshotone.com.

1. What are the main differences between Selenium, pyppeteer, and Playwright?

  1. Selenium is a robust library for browser automation that supports multiple browsers and is great for web scraping, testing, and automation tasks. It requires a separate driver for each browser.
  2. pyppeteer is a Python port of Puppeteer (a headless Chrome/Chromium browser automation library), but it’s outdated and not maintained. It’s simpler for Chrome/Chromium-specific tasks but less versatile compared to Selenium and Playwright.
  3. Playwright is a modern tool that supports all modern web browsers, including Chrome, Firefox, and Safari. It’s designed for automated testing, browser automation, and is capable of taking screenshots, generating PDFs, and more. It’s the most recommended tool for new projects due to its extensive features and active development.

2. Can I use these tools for automating tasks other than taking screenshots?

Yes, all three tools—Selenium, pyppeteer, and Playwright—are capable of automating a wide range of browser tasks beyond taking screenshots, such as web scraping, automated testing, and simulating user interactions with web pages.

3. Are there any cost implications for using Playwright or Selenium compared to using a URL to Screenshot API like ScreenshotOne?

Using Playwright or Selenium primarily incurs no direct costs as they are open-source tools; however, infrastructure and maintenance costs may apply, especially as you scale. In contrast, a URL to Screenshot API like ScreenshotOne might have a free tier, but will typically have costs associated with higher usage tiers, offering the benefit of managed infrastructure.

4. How do I handle dynamic content or login-required pages when taking screenshots?

For dynamic content, using Playwright or Selenium allows for executing JavaScript or waiting for elements to load before taking screenshots. For login-required pages, scripts can be written to automate the login process before taking a screenshot. Care should be taken to manage credentials securely.

5. What should I do if I encounter a specific error or issue while using one of these tools?

For detailed technical issues not covered in the post, it’s best to consult the official documentation or communities for each tool (e.g., GitHub issues, Stack Overflow). For Playwright and Selenium, they have active communities where users can ask for help.

For issues related to ScreenshotOne, reaching out to our support as mentioned in the post would be the recommended course of action.