Take website screenshots with Python

Learn how to take webpage screenshots in Python with Playwright, including full-page screenshots, common fixes, and production-ready API options.

Blog post 8 min read

Written by

Dmytro Krasun

Updated on

The easiest way to take a screenshot of a webpage in Python is with Playwright. Install Playwright, open the URL, and call page.screenshot(). Use full_page=True to capture the entire page:

from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page()
page.goto("https://example.com")
page.screenshot(path="screenshot.png", full_page=True)
browser.close()

That is the modern answer for most Python webpage screenshot tasks. You can still use Selenium if it is already in your stack, or pyppeteer if you maintain an older script, but Playwright is the best default choice today.

However, if you do not want to manage and scale browsers yourself, or deal with blocking banners, ads, and other screenshot edge cases, consider using the ScreenshotOne API. It handles these issues and more.

Overview

Playwright is the best library to automate website screenshots in Python today. It is easy to install, actively maintained, and supports full-page screenshots without extra work.

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. Playwright, a modern browser automation library for screenshots, testing, scraping, and PDF generation.
  2. An old but battle-tested automation testing framework like Selenium with a web driver of choice.
  3. 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.
  4. You can use any URL to screenshot API to quickly start without managing browsers and get 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.

OptionBest for
PlaywrightLocal scripts, automated tests, and full-page screenshots.
SeleniumExisting Selenium test suites and cross-browser automation.
pyppeteerMaintaining legacy Puppeteer-style Python scripts.
ScreenshotOne APIProduction screenshots, scale, queues, retries, and managed browsers.

Playwright in Python

Playwright is specifically designed to meet the requirements of end-to-end testing, but it is also a practical tool for screenshot automation. It supports Chromium, WebKit, and Firefox, and can capture full-page screenshots natively. It is one of the ways if you render screenshots without Selenium.

Playwright offers support for contemporary rendering engines, such as Chromium, WebKit, and Firefox. It works across 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 powerful library and it can do mostly everything that a browser can do:

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

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

First, install it:

Terminal window
pip install playwright
playwright install

Then take a screenshot:

from playwright.sync_api import sync_playwright
def run(playwright):
# launch the browser
browser = playwright.chromium.launch()
# open 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.

Python full-page screenshot

Playwright can capture a full-page screenshot with the full_page=True option:

from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page(viewport={"width": 1280, "height": 720})
page.goto("https://example.com", wait_until="networkidle")
page.screenshot(path="full-page.png", full_page=True)
browser.close()

Set the viewport explicitly when the rendered layout matters. Many responsive websites show different navigation, typography, and content at different viewport sizes.

If you do not want to use Selenium, use Playwright. It does not require a separate browser driver like chromedriver, and it provides a cleaner screenshot API:

from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page()
page.goto("https://example.com")
page.screenshot(path="without-selenium.png")
browser.close()

For new projects, Playwright is usually a better fit than pyppeteer because it is actively maintained and supports Chromium, WebKit, and Firefox.

Selenium in Python

Use Selenium for rendering screenshots in Python if you already have that library in your stack. Otherwise, if the goal is to render screenshots only, go with Playwright. Prefer Playwright to Selenium for new testing projects also, since it 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 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

The pyppeteer library is outdated and not maintained anymore. For simple legacy 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 website 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

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 only a few screenshots periodically, I would go with Selenium, pyppeteer or Playwright. But if you need scale or don’t want to deal with infrastructure issues, consider using our ScreenshotOne API—an URL to screenshot API.

Use Playwright when you need a local script or test automation. Use a screenshot API when screenshots are part of production infrastructure and you need retries, browser maintenance, blocking banners, proxies, queues, or predictable rendering at scale.

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 we are happy to help with screenshot automation even if you are not a ScreenshotOne customer and don’t plan to be.

Common problems when screenshotting webpages in Python

You might encounter a few issues while using Playwright, Selenium, or any other browser automation library. Here are the most common ones:

  1. Blank screenshots: wait until the page loads before taking a screenshot. In Playwright, try page.goto(url, wait_until="networkidle") or wait for a specific selector with page.wait_for_selector().
  2. Lazy-loaded content is missing: scroll the page or wait for the content that should appear before taking the screenshot.
  3. Cookie banners hide the page: close them with browser automation, block them with custom rules, or use a screenshot API that can block cookie banners.
  4. Chat widgets cover important content: hide them with CSS, block their scripts, or use a managed screenshot API that supports blocking chat widgets.
  5. The screenshot has the wrong layout: set a fixed viewport size before opening the page.
  6. Full-page screenshots are too tall or unstable: test the page at a realistic viewport and avoid infinite-scroll pages unless you control the scroll behavior.
  7. Login-required pages fail: log in first, reuse a browser context with saved cookies, and never hardcode credentials in source code.

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 a screenshot API.

For local scripts and tests, I would choose Playwright. If you need to take screenshots at scale and don’t want to deal with browser infrastructure and corner cases, then give a try to a 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

If you read the article, but still have questions. Please, check the most frequently asked. And if you still have questions, feel free reach out at support@screenshotone.com.

What is the best Python library for taking webpage screenshots?

Playwright is the best default choice for taking webpage screenshots in Python today. It is actively maintained, supports Chromium, WebKit, and Firefox, and can capture full-page screenshots with page.screenshot(full_page=True).

Can I take a screenshot of a webpage in Python without Selenium?

Yes. Use Playwright if you want to take webpage screenshots without Selenium. It does not require a separate browser driver like chromedriver and provides a clean screenshot API for normal and full-page screenshots.

What is the difference between Selenium, pyppeteer, and Playwright?

Selenium is useful if you already use it for browser automation or testing. pyppeteer is an older Python port of Puppeteer and is no longer the best choice for new projects. Playwright is the modern default for Python webpage screenshots because it is actively maintained and supports multiple browser engines.

How do I take a full-page screenshot in Python?

With Playwright, call page.screenshot(path='screenshot.png', full_page=True). Set the viewport size first if the page has a responsive layout and wait for the page or target selector before capturing.

When should I use a screenshot API instead of Playwright?

Use Playwright for local scripts and tests. Use a screenshot API like ScreenshotOne when screenshots are part of production infrastructure and you need managed browsers, retries, queues, proxy support, blocking cookie banners or ads, and predictable rendering at scale.

Read more Screenshot rendering

Interviews, tips, guides, industry best practices, and news.

View all posts
Let's build a screenshot API

Let's build a screenshot API

Two years have passed since the launch of ScreenshotOne, and I want to do a fun coding exercise and build a tiny subset of what the API is today, but from scratch.

Read more

Automate website screenshots

Exhaustive documentation, ready SDKs, no-code tools, and other automation to help you render website screenshots and outsource all the boring work related to that to us.