Skip to main content

🎭 Playwright for Python PyPI version Anaconda version Join Slack

Docs | API

Playwright is a Python library to automate Chromium, Firefox and WebKit browsers with a single API. Playwright delivers automation that is ever-green, capable, reliable and fast. See how Playwright is better.

Linux macOS Windows
Chromium 92.0.4498.0 ✅ ✅ ✅
WebKit 14.2 ✅ ✅ ✅
Firefox 89.0b6 ✅ ✅ ✅

Headless execution is supported for all browsers on all platforms.

Usage - pip

PyPI version

pip install playwright
playwright install

This installs Playwright and browser binaries for Chromium, Firefox and WebKit. Playwright requires Python 3.7+.

Usage - conda

Anaconda version

conda config --add channels conda-forge
conda config --add channels microsoft
conda install playwright
playwright install

This installs Playwright and browser binaries for Chromium, Firefox and WebKit with the conda package manager. Playwright requires a conda environment with Python 3.7+.

Record and generate code

Playwright can record user interactions in a browser and generate code. See demo.

# Pass --help to see all options
playwright codegen

Playwright offers both sync (blocking) API and async API. They are identical in terms of capabilities and only differ in how one consumes the API.

Sync API

This is our default API for short snippets and tests. If you are not using asyncio in your application, it is the easiest to use Sync API notation.

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://whatsmyuseragent.org/')
        page.screenshot(path=f'example-{browser_type.name}.png')
        browser.close()

Async API

If your app is based on the modern asyncio loop and you are used to async/await constructs, Playwright exposes Async API for you. You should use this API inside a Python REPL supporting asyncio like with python -m asyncio

$ python -m asyncio
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://whatsmyuseragent.org/')
            await page.screenshot(path=f'example-{browser_type.name}.png')
            await browser.close()

asyncio.run(main())

With pytest

Use our pytest plugin for Playwright.

def test_playwright_is_visible_on_google(page):
    page.goto("https://www.google.com")
    page.type("input[name=q]", "Playwright GitHub")
    page.click("input[type=submit]")
    page.wait_for_selector("text=microsoft/Playwright")

Interactive mode (REPL)

Blocking REPL, as in CLI:

>>> from playwright.sync_api import sync_playwright
>>> playwright = sync_playwright().start()

# Use playwright.chromium, playwright.firefox or playwright.webkit
# Pass headless=False to see the browser UI
>>> browser = playwright.chromium.launch()
>>> page = browser.new_page()
>>> page.goto("http://whatsmyuseragent.org/")
>>> page.screenshot(path="example.png")
>>> browser.close()
>>> playwright.stop()

Async REPL such as asyncio REPL:

$ python -m asyncio
>>> from playwright.async_api import async_playwright
>>> playwright = await async_playwright().start()
>>> browser = await playwright.chromium.launch()
>>> page = await browser.new_page()
>>> await page.goto("http://whatsmyuseragent.org/")
>>> await page.screenshot(path="example.png")
>>> await browser.close()
>>> await playwright.stop()

Examples

Mobile and geolocation

This snippet emulates Mobile Safari on a device at a given geolocation, navigates to maps.google.com, performs action and takes a screenshot.

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    iphone_11 = p.devices["iPhone 11 Pro"]
    browser = p.webkit.launch(headless=False)
    context = browser.new_context(
        **iphone_11,
        locale="en-US",
        geolocation={"longitude": 12.492507, "latitude": 41.889938 },
        permissions=["geolocation"]
    )
    page = context.new_page()
    page.goto("https://maps.google.com")
    page.click("text=Your location")
    page.screenshot(path="colosseum-iphone.png")
    browser.close()
Async variant
import asyncio
from playwright.async_api import async_playwright

async def main():
    async with async_playwright() as p:
        iphone_11 = p.devices["iPhone 11 Pro"]
        browser = await p.webkit.launch(headless=False)
        context = await browser.new_context(
            **iphone_11,
            locale="en-US",
            geolocation={"longitude": 12.492507, "latitude": 41.889938},
            permissions=["geolocation"]
        )
        page = await context.newPage()
        await page.goto("https://maps.google.com")
        await page.click("text="Your location"")
        await page.screenshot(path="colosseum-iphone.png")
        await browser.close()

asyncio.run(main())

Evaluate JS in browser

This code snippet navigates to example.com in Firefox, and executes a script in the page context.

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.firefox.launch()
    page = browser.new_page()
    page.goto("https://www.example.com/")
    dimensions = page.evaluate("""() => {
      return {
        width: document.documentElement.clientWidth,
        height: document.documentElement.clientHeight,
        deviceScaleFactor: window.devicePixelRatio
      }
    }""")
    print(dimensions)
    browser.close()
Async variant
import asyncio
from playwright.async_api import async_playwright

async def main():
    async with async_playwright() as p:
        browser = await p.firefox.launch()
        page = await browser.new_page()
        await page.goto("https://www.example.com/")
        dimensions = await page.evaluate("""() => {
          return {
            width: document.documentElement.clientWidth,
            height: document.documentElement.clientHeight,
            deviceScaleFactor: window.devicePixelRatio
          }
        }""")
        print(dimensions)
        await browser.close()

asyncio.run(main())

Intercept network requests

This code snippet sets up request routing for a Chromium page to log all network requests.

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch()
    page = browser.new_page()

    def log_and_continue_request(route, request):
        print(request.url)
        route.continue_()

    # Log and continue all network requests
    page.route("**/*", log_and_continue_request)

    page.goto("http://todomvc.com")
    browser.close()
Async variant
import asyncio
from playwright.async_api import async_playwright

async def main():
    async with async_playwright() as p:
        browser = await p.chromium.launch()
        page = await browser.new_page()

        async def log_and_continue_request(route, request):
            print(request.url)
            await route.continue_()

        # Log and continue all network requests
        await page.route("**/*", log_and_continue_request)
        await page.goto("http://todomvc.com")
        await browser.close()

asyncio.run(main())

Documentation

Check out our new documentation site!

Metadata

Release files for playwright 1.11.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for playwright 1.11.1
File Actions
playwright-1.11.1-py3-none-win_amd64.whl Python 3 none Windows x86-64 Details for playwright-1.11.1-py3-none-win_amd64.whl
playwright-1.11.1-py3-none-win32.whl Python 3 none Windows x86-32 Details for playwright-1.11.1-py3-none-win32.whl
playwright-1.11.1-py3-none-manylinux1_x86_64.whl Python 3 none Linux glibc 2.5+ x86-64 Details for playwright-1.11.1-py3-none-manylinux1_x86_64.whl
playwright-1.11.1-py3-none-macosx_11_0_universal2.whl Python 3 none macOS 11.0+ universal2 (ARM64, x86-64) Details for playwright-1.11.1-py3-none-macosx_11_0_universal2.whl
playwright-1.11.1-py3-none-macosx_10_13_x86_64.whl Python 3 none macOS 10.13+ x86-64 Details for playwright-1.11.1-py3-none-macosx_10_13_x86_64.whl

Total release size: 83.9 MB

Release files / playwright-1.11.1-py3-none-win_amd64.whl

Download URL playwright-1.11.1-py3-none-win_amd64.whl
Size 14.8 MB
Tags Python 3 Windows x86-64
SHA-256 checksum
How to use checksums
3007c694a076fa51a645518b84006674d6f2977a3c9f0520882bb489195e1428
BLAKE2b-256 checksum
How to use checksums
c1ff54073aacf06ccaee5c50468575d6a3d8e75247d407b47631d4be90a041b8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/56.1.0 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

Release files / playwright-1.11.1-py3-none-win32.whl

Download URL playwright-1.11.1-py3-none-win32.whl
Size 13.2 MB
Tags Python 3 Windows x86-32
SHA-256 checksum
How to use checksums
233ca65914d45ee05061d34b0a72a94939f9fe21da6f3077db20be69ab588117
BLAKE2b-256 checksum
How to use checksums
e935fe8f3c87f082266a754bc0f23c97a438029d30c916618baee55cdda9119e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/56.1.0 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

Release files / playwright-1.11.1-py3-none-manylinux1_x86_64.whl

Download URL playwright-1.11.1-py3-none-manylinux1_x86_64.whl
Size 20.1 MB
Tags Linux glibc 2.5+ x86-64 Python 3
SHA-256 checksum
How to use checksums
adb0038bb223df83c66e679a7e029c4d7452505c1270aced281561d213f11a25
BLAKE2b-256 checksum
How to use checksums
1128a9c1eb34e46e4ec1408322777b52fee835e8db98f12432f7b8aef30012c3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/56.1.0 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

Release files / playwright-1.11.1-py3-none-macosx_11_0_universal2.whl

Download URL playwright-1.11.1-py3-none-macosx_11_0_universal2.whl
Size 17.9 MB
Tags Python 3 macOS 11.0+ universal2 (ARM64, x86-64)
SHA-256 checksum
How to use checksums
be7353afbca8610215e898453efeff9d54d14c9b4b3bd07a30b7f357a3d916ab
BLAKE2b-256 checksum
How to use checksums
e0bb2e725fa155704a6025df911465ff76926bf5fe82beef880bf4316054cd54
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/56.1.0 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

Release files / playwright-1.11.1-py3-none-macosx_10_13_x86_64.whl

Download URL playwright-1.11.1-py3-none-macosx_10_13_x86_64.whl
Size 17.9 MB
Tags Python 3 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
3a889c2ea85e1801ee5d0d1c11e1c87a5a740f5ae88d09a4a34331d7ceed0028
BLAKE2b-256 checksum
How to use checksums
c910c78e68f3fd614a4c47948997fad1e11960bd8b50b713cecfc9fd2ebe32f0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/56.1.0 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

Security for playwright 1.11.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.11.1 This release

5 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page
⌂ 换网址