Skip to main content

Capture screenshots using Playwright

This example demonstrates how to capture screenshots of web pages using PlaywrightCrawler and store them in the key-value store.

The PlaywrightCrawler is configured to automate the browsing and interaction with web pages. It uses headless Chromium as the browser type to perform these tasks. Each web page specified in the initial list of URLs is visited sequentially, and a screenshot of the page is captured using Playwright's page.screenshot() method.

The captured screenshots are stored in the key-value store, which is suitable for managing and storing files in various formats. In this case, screenshots are stored as PNG images with a unique key generated from the URL of the page.

import asyncio

from crawlee.playwright_crawler import PlaywrightCrawler, PlaywrightCrawlingContext
from crawlee.storages import KeyValueStore

async def main() -> None:
crawler = PlaywrightCrawler(
# Limit the crawl to max requests. Remove or increase it for crawling all links.
# Headless mode, set to False to see the browser in action.
# Browser types supported by Playwright.

# Open the default key-value store.
kvs = await

# Define the default request handler, which will be called for every request.
async def request_handler(context: PlaywrightCrawlingContext) -> None:'Processing {context.request.url} ...')

# Capture the screenshot of the page using Playwright's API.
screenshot = await
name = context.request.url.split('/')[-1]

# Store the screenshot in the key-value store.
await kvs.set_value(

# Run the crawler with the initial list of URLs.

if __name__ == '__main__':