Skip to content

Usage

Minimal example: screenshot

This is the canonical minimal script from the official repository. It launches a headless Chromium browser, navigates to a page, takes a screenshot, and closes the browser.

python
import asyncio
from pyppeteer import launch

async def main():
    browser = await launch()
    page = await browser.newPage()
    await page.goto('https://example.com')
    await page.screenshot({'path': 'example.png'})
    await browser.close()

asyncio.get_event_loop().run_until_complete(main())

Save as screenshot.py and run:

bash
python screenshot.py

A file named example.png is written to the current directory.

Common launch options

python
browser = await launch(
    headless=True,           # False to see the browser window
    args=['--no-sandbox'],   # Required on some Linux environments
    executablePath=None,     # Path to a custom Chromium/Chrome binary
)

Wait for navigation

python
page = await browser.newPage()
await page.goto('https://example.com', {'waitUntil': 'networkidle0'})

waitUntil options: 'load', 'domcontentloaded', 'networkidle0', 'networkidle2'

Click and type

python
await page.click('#search-input')
await page.type('#search-input', 'pyppeteer', {'delay': 50})
await page.keyboard.press('Enter')
await page.waitForNavigation()

Extract page content

python
title = await page.title()
content = await page.content()          # full HTML
text = await page.evaluate('document.body.innerText')

PDF generation

python
await page.pdf({'path': 'output.pdf', 'format': 'A4'})

Evaluate JavaScript

python
result = await page.evaluate('() => window.location.href')
print(result)

Notes

  • All pyppeteer API calls are async/await coroutines.
  • Method names mirror Puppeteer (JavaScript) exactly, using camelCase.
  • On Linux CI environments, add --no-sandbox and --disable-setuid-sandbox to args.
  • For anti-bot scenarios, pyppeteer has no built-in stealth; use puppeteer-extra-stealth equivalents or switch to Playwright.