DEV Community

bertrand
bertrand

Posted on • Originally published at screenshotmax.com

How to take website screenshots with Python

Learn how to take website screenshots with Python. Explore Selenium, Playwright, Pyppeteer, and Screenshot API as a Service with Python SDK.

Capturing website screenshots is a fundamental task for testing, monitoring, and content automation. In this guide, we explore four methods, including Selenium, Playwright, Pyppeteer, and Screenshot API, comparing their strengths, and weaknesses, and providing full Python examples. Whether you need precision control or a hassle-free SaaS integration, this article helps you choose the right approach.

Selenium for Python

Selenium is one of the oldest and most trusted browser automation tools in Python. With roots in the early 2000s, Selenium was created to automate web applications for testing purposes. Powered by real web browsers (Chrome, Firefox, Safari, Edge), it simulates entire user sessions, ensuring high fidelity.

Installation & basic usage

    pip install selenium webdriver-manager
Enter fullscreen mode Exit fullscreen mode
    from selenium import webdriver
    from selenium.webdriver.chrome.service import Service
    from webdriver_manager.chrome import ChromeDriverManager

    driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
    driver.get('https://example.com')
    driver.save_screenshot('selenium_screenshot.png')
    driver.quit()
    print("Selenium: Screenshot saved")
Enter fullscreen mode Exit fullscreen mode

Pros & cons

Selenium excels at faithfully emulating real user interactions using full browser engines, ideal for complex UI and dynamic content. However, its reliance on external browser drivers and the need to manage browser instances can make it heavier and slower than headless solutions.

Playwright for Python

Developed by Microsoft and released in 2020, Playwright is a relative newcomer optimized for end-to-end testing. It supports Chromium, Firefox, and WebKit with consistent APIs and excels at handling modern web apps with built-in network and script control.

Installation & basic usage

    pip install playwright
    playwright install
Enter fullscreen mode Exit fullscreen mode
    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='playwright_screenshot.png', full_page=True)
      browser.close()
    print("Playwright: Screenshot saved")
Enter fullscreen mode Exit fullscreen mode

Pros & cons

Playwright delivers fast, reliable automation with broader browser support and powerful features like intercepting network traffic and built-in wait mechanisms. Yet, the necessity to install browser binaries and its larger package size may be overkill for simple tasks.

Pyppeteer (Python port of Puppeteer)

Pyppeteer is the Python port of Puppeteer,a tool built by the Chromium team for controlling headless Chrome or Chromium. Emerging around 2018, it brings Puppeteer’s ease-of-use and high-speed operations to Python developers.

Installation & basic Usage

    pip install pyppeteer
Enter fullscreen mode Exit fullscreen mode
    import asyncio
    from pyppeteer import launch

    async def capture():
      browser = await launch()
      page = await browser.newPage()
      await page.goto('https://example.com')
      await page.screenshot({'path': 'pyppeteer_screenshot.png', 'fullPage': True})
      await browser.close()
      print("Pyppeteer: Screenshot saved")

    asyncio.get_event_loop().run_until_complete(capture())
Enter fullscreen mode Exit fullscreen mode

Pros & cons

Pyppeteer offers a lean headless Chromium experience with high-speed execution and easy scripting. On the downside, maintenance lag and dependency on a specific Chromium version can pose compatibility issues, especially as web technologies evolve.

Screenshot API as a service with Python

ScreenshotMAX is a lightweight Screenshot as a Service (SaaS) API designed to simplify and scale screenshot capture. Launched to eliminate local browser setup complexities, this service offers a stable endpoint where you send a URL and receive an image in return. With an accompanying Python SDK, ScreenshotMAX integrates effortlessly into your workflow.

Installation & basic usage

    pip install ScreenshotMAX
Enter fullscreen mode Exit fullscreen mode
    from ScreenshotMAX import SDK
    from screenshotmax.enum import ImageFormat
    from screenshotmax.options import ScreenshotOptions

    # Initialize SDK
    sdk = SDK('YOUR_ACCESS_KEY', 'YOUR_SECRET_KEY')

    # Set up options
    opts = ScreenshotOptions(url="https://example.com", format=ImageFormat.PNG)
    # fetch screenshot
    result, headers = sdk.screenshot.set_options(opts).fetch()

    # save screenshot to file
    with open("screenshot.png", "wb") as f:
      f.write(result)
Enter fullscreen mode Exit fullscreen mode

Pros & cons

ScreenshotMAX abstracts away browser management, offering fast, scalable screenshots with zero local dependencies. It’s ideal for bulk capture, monitoring, and cloud environments. However, it introduces network latency, billing considerations, and potential limits compared to free, entirely local options.

Comparison Overview

Method Setup Complexity Speed Browser Fidelity Scalability
Selenium High Slowest Very High Low–Medium
Playwright Medium Fast High Medium
Pyppeteer Medium Fastest High Medium
Screenshot API Very Low Fast–Medium Medium Very High
  • Selenium requires managing browser drivers, making it less ideal for mass automation.
  • Playwright offers excellent speed and multimedia handling but includes heavier dependencies.
  • Pyppeteer trades some maintenance reliability for a minimalistic, direct Chromium approach.
  • ScreenshotMAX API eliminates local complexity, trading off raw control and introducing API limits and costs.

Which should you choose?

Use Selenium if you need full browser interactions, real user simulations, or testing in real-world environments. Choose Playwright for fast and robust automated testing across multiple browser engines. Opt for Pyppeteer if you want a lightweight, headless Chromium script without larger frameworks. And pick ScreenshotMAX API when you’re dealing with cloud pipelines, scalable capture needs, or want to skip installing and maintaining browsers.

If you need to automate complex browser interactions or generate screenshots in tightly controlled environments, Selenium or Playwright offers fine-grained control. It’s ideal for one-off screenshots, local development, or workflows where you need to manipulate the DOM or wait for dynamic content.

However, if your goal is to capture hundreds or thousands of screenshots reliably, especially in a production setting, a screenshot API like ScreenshotMAX is a better choice. It handles the infrastructure, parallel processing, and scaling for you, so you don’t have to worry about memory limits, browser crashes, or queueing jobs manually. You just call the API and get your result back quickly, with consistent rendering and high availability. It’s purpose-built for bulk screenshot tasks and large workloads.

No matter your needs, this guide empowers you to take high-quality website screenshots using Python, whether open-source or SaaS-powered.

Originally published at: https://screenshotmax.com/blog/how-to-take-website-screenshots-with-python

Top comments (0)