Playwright for Python
repository·main·Indexed 12 days ago
https://github.com/microsoft/playwright-pythonA 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.
What's inside Playwright
- Playwright is a Python library designed to automate Chromium, Firefox, and WebKit browsers using a single API. It is cross-platform and supports Linux, macOS, and Windows.
Fix typing issues with Playwright ToT
mainIf you need to fix typing issues using the Playwright Top-of-Tree (ToT) source, follow these steps to generate and update the API JSON:
- Generate the API JSON from the Playwright source:
API_JSON_MODE=1 node ../playwright/utils/doclint/generateApiJson.js > /tmp/api.json - 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- Generate the API JSON from the Playwright source:
Roll Playwright-Python to the latest Playwright driver
mainTo update the
playwright-pythonrepository to use a newer version of the Playwright driver (theplaywright-corenpm 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/playwrightrepository at the target version (v<new>) to generate the API.
Steps
- 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 . - Update Driver Version:
Update the
DRIVER_VERSIONin the repository (this corresponds to theplaywright-corenpm version, e.g.,1.61.0). - Refresh Node Version:
python scripts/update_node_version.py - Download New Driver:
Use the build command to fetch
playwright-corefrom npm and the matching Node.js binary to assemble the bundle:
Note: You can setpython -m build --wheelnpm_config_registryto use a different npm registry. - Generate API:
Point to your local
microsoft/playwrightcheckout using thePW_SRC_DIRenvironment variable:PW_SRC_DIR=../playwright ./scripts/update_api.sh - 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.shHow to get help with Playwright for Python
mainIf 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-playwrightforum 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.
Install and use Playwright for Python
mainPlaywright is a Python library used to automate Chromium, Firefox, and WebKit browsers using a single API. It is designed for reliable, fast, and cross-browser web automation.Use Playwright for Python with Asynchronous API
mainFor applications that require non-blocking I/O or integration with
asyncio, Playwright provides an asynchronous API. Useasync_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())Use Playwright for Python with Synchronous API
mainPlaywright 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()Use the Playwright CLI via the python module
mainThe
playwrightmodule can be invoked as a CLI entrypoint usingpython -m playwright. This allows you to run Playwright's internal driver commands (such asinstall,codegen, orshow-report) directly from your terminal. The CLI passes all arguments provided afterplaywrightto the underlying Playwright driver.# Example: Installing browsers python -m playwright install # Example: Running code generation python -m playwright codegen