Playwright for Python

repository·main·Indexed 12 days ago

https://github.com/microsoft/playwright-python

A high-level automation library to control Chromium, Firefox, and WebKit browsers through a unified API. It supports both synchronous and asynchronous programming models and is cross-platform for Linux, macOS, and Windows.

Tokens
1.5K
Snippets
5
Records
8
Agent score
95%

What's inside Playwright

  1. Fix typing issues with Playwright ToT

    main

    If you need to fix typing issues using the Playwright Top-of-Tree (ToT) source, follow these steps to generate and update the API JSON:

    1. Generate the API JSON from the Playwright source:
      API_JSON_MODE=1 node ../playwright/utils/doclint/generateApiJson.js > /tmp/api.json
    2. Update the API using the generated file:
      PW_API_JSON=/tmp/api.json ./scripts/update_api.sh
    API_JSON_MODE=1 node ../playwright/utils/doclint/generateApiJson.js > /tmp/api.json
    PW_API_JSON=/tmp/api.json ./scripts/update_api.sh
  2. Roll Playwright-Python to the latest Playwright driver

    main

    To update the playwright-python repository to use a newer version of the Playwright driver (the playwright-core npm version), follow these steps. This process involves updating the driver pin, downloading the new driver bundle, and regenerating the API definitions.

    Prerequisites

    • Python 3.10 or newer.
    • A local checkout of the microsoft/playwright repository at the target version (v<new>) to generate the API.

    Steps

    1. Setup Environment:
      git clone https://github.com/microsoft/playwright-python
      python -m venv env
      source env/bin/activate
      python -m pip install --upgrade pip
      pip install -r local-requirements.txt
      pre-commit install
      pip install -e .
    2. Update Driver Version: Update the DRIVER_VERSION in the repository (this corresponds to the playwright-core npm version, e.g., 1.61.0).
    3. Refresh Node Version:
      python scripts/update_node_version.py
    4. Download New Driver: Use the build command to fetch playwright-core from npm and the matching Node.js binary to assemble the bundle:
      python -m build --wheel
      Note: You can set npm_config_registry to use a different npm registry.
    5. Generate API: Point to your local microsoft/playwright checkout using the PW_SRC_DIR environment variable:
      PW_SRC_DIR=../playwright ./scripts/update_api.sh
    6. Submit: Commit the changes and submit a PR.
    # Summary of the rolling process
    # 1. Update DRIVER_VERSION
    # 2. python scripts/update_node_version.py
    # 3. python -m build --wheel
    # 4. PW_SRC_DIR=../playwright ./scripts/update_api.sh
  3. How to get help with Playwright for Python

    main

    If you need assistance using Playwright for Python, you can access support through the following channels:

    • Documentation: The official docs site contains guides and API references.
    • Community Support: Join the Discord Server and use the help-playwright forum to ask questions and connect with other developers.
    • GitHub Issues: For reporting bugs or requesting features, use the GitHub issues page. Always search for existing issues before filing a new one to prevent duplicates.
  4. Use Playwright for Python with Asynchronous API

    main

    For applications that require non-blocking I/O or integration with asyncio, Playwright provides an asynchronous API. Use async_playwright() as an async context manager. All browser and page operations must be awaited.

    import asyncio
    from playwright.async_api import async_playwright
    
    async def main():
        async with async_playwright() as p:
            for browser_type in [p.chromium, p.firefox, p.webkit]:
                browser = await browser_type.launch()
                page = await browser.new_page()
                await page.goto('http://playwright.dev')
                await page.screenshot(path=f'example-{browser_type.name}.png')
                await browser.close()
    
    asyncio.run(main())
  5. Use Playwright for Python with Synchronous API

    main

    Playwright provides a synchronous API for automation tasks that do not require an event loop. You can use sync_playwright() as a context manager to manage the Playwright instance. This is useful for simple scripts and linear automation flows.

    from playwright.sync_api import sync_playwright
    
    with sync_playwright() as p:
        for browser_type in [p.chromium, p.firefox, p.webkit]:
            browser = browser_type.launch()
            page = browser.new_page()
            page.goto('http://playwright.dev')
            page.screenshot(path=f'example-{browser_type.name}.png')
            browser.close()
  6. Use the Playwright CLI via the python module

    main

    The playwright module can be invoked as a CLI entrypoint using python -m playwright. This allows you to run Playwright's internal driver commands (such as install, codegen, or show-report) directly from your terminal. The CLI passes all arguments provided after playwright to the underlying Playwright driver.

    # Example: Installing browsers
    python -m playwright install
    
    # Example: Running code generation
    python -m playwright codegen