Capturing website screenshots programmatically is one of the most common automation tasks for developers. Whether you're building a link previewer, monitoring a competitor's page, or generating OG images, you need a reliable way to render a webpage and save it as an image.
This guide covers four approaches — from the fastest (an API call) to the most hands-on (a headless browser you manage yourself). Each includes working Python code you can copy and run.
Method 1: Rendex API (3 Lines of Code)
The fastest path. The Rendex Python SDK handles browser rendering on Cloudflare's edge network — no Chromium install, no memory management, no cold starts.
# pip install rendex
from rendex import Rendex
from pathlib import Path
rendex = Rendex("YOUR_API_KEY")
result = rendex.screenshot("https://example.com")
Path("screenshot.png").write_bytes(result.image)
print(f"{result.metadata.bytes_size} bytes in {result.metadata.load_time_ms}ms")
Async support is built in — ideal for batch jobs:
import asyncio
from rendex import AsyncRendex
from pathlib import Path
async def capture_many(urls: list[str]):
async with AsyncRendex("YOUR_API_KEY") as rendex:
for url in urls:
result = await rendex.screenshot(url, format="webp", full_page=True)
slug = url.replace("https://", "").replace("/", "_")
Path(f"{slug}.webp").write_bytes(result.image)
asyncio.run(capture_many([
"https://github.com",
"https://news.ycombinator.com",
"https://stripe.com",
]))
Advanced options include dark mode, ad blocking, element selectors, custom viewports, and WebP output. See the full API reference.
When to use: Production workloads, AI agent pipelines, batch processing. You get 500 free calls/month to start. Get an API key.
Method 2: Playwright
Microsoft's Playwright is the current gold standard for headless browser automation. It supports Chromium, Firefox, and WebKit, with an excellent Python API.
# pip install playwright
# python -m playwright install chromium
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="screenshot.png", full_page=True)
browser.close()
Playwright gives you full control — you can click buttons, fill forms, wait for specific elements, and intercept network requests before capturing.
When to use: You need browser interaction before capture (login flows, cookie banners, SPAs that require clicks). You manage the infrastructure yourself.
Trade-offs: Requires a Chromium install (~400 MB), consumes significant memory per browser instance, and cold starts can be slow in serverless environments.
Method 3: Selenium
Selenium has been the go-to browser automation tool since 2004. If your team already uses it for testing, adding screenshot capture is straightforward.
# pip install selenium webdriver-manager
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
options = webdriver.ChromeOptions()
options.add_argument("--headless=new")
options.add_argument("--window-size=1280,720")
options.add_argument("--disable-gpu")
driver = webdriver.Chrome(
service=Service(ChromeDriverManager().install()),
options=options,
)
driver.get("https://example.com")
driver.save_screenshot("screenshot.png")
driver.quit()
When to use: Existing Selenium test suites where you want to add screenshot capture without introducing a new tool.
Trade-offs: Slower than Playwright, less reliable waiting mechanisms, and the WebDriver protocol adds overhead. Selenium 4+ improved significantly, but Playwright is generally preferred for new projects.
Method 4: Pyppeteer
Pyppeteer is a Python port of Puppeteer (Node.js). It uses the Chrome DevTools Protocol directly, which gives fine-grained control.
# pip install pyppeteer
import asyncio
from pyppeteer import launch
async def capture():
browser = await launch(headless=True)
page = await browser.newPage()
await page.setViewport({"width": 1280, "height": 720})
await page.goto("https://example.com", waitUntil="networkidle0")
await page.screenshot({"path": "screenshot.png", "fullPage": True})
await browser.close()
asyncio.run(capture())
When to use: You need DevTools Protocol access for advanced features like performance tracing or network interception, and prefer Python over Node.js.
Trade-offs: The project is less actively maintained than Playwright. Consider Playwright for new projects.
Which Method Should You Choose?
| Method | Setup Time | Infra Needed | Best For |
|---|---|---|---|
| Rendex API | 30 seconds | None | Production, AI agents, batch jobs |
| Playwright | 5 minutes | Chromium install | Browser interaction before capture |
| Selenium | 10 minutes | ChromeDriver | Existing Selenium test suites |
| Pyppeteer | 5 minutes | Chromium install | DevTools Protocol access |
For most production use cases — especially if you're building an AI agent pipeline or need screenshots at scale — an API is the right choice. You avoid managing browsers, handling memory, and dealing with cold starts.
Try the free screenshot tool to see Rendex in action without writing any code, or grab an API key and start with 500 free calls/month.
Want to see how Rendex compares to other APIs? See the 2026 comparison.
Top comments (0)