Skip to main content

A high-level API to automate web browsers

Project description

🎭 Playwright for Python PyPI 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 install playwright
playwright install

This installs Playwright and browser binaries for Chromium, Firefox and WebKit. Playwright requires 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!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

playwright-1.11.0-py3-none-win_amd64.whl (14.8 MB view details)

Uploaded Python 3 Windows x86-64

playwright-1.11.0-py3-none-win32.whl (13.2 MB view details)

Uploaded Python 3 Windows x86

playwright-1.11.0-py3-none-manylinux_2_27_x86_64.whl (20.1 MB view details)

Uploaded Python 3 manylinux: glibc 2.27+ x86-64

playwright-1.11.0-py3-none-macosx_11_0_universal2.whl (17.9 MB view details)

Uploaded Python 3 macOS 11.0+ universal2 (ARM64, x86-64)

playwright-1.11.0-py3-none-macosx_10_13_x86_64.whl (17.9 MB view details)

Uploaded Python 3 macOS 10.13+ x86-64

File details

Details for the file playwright-1.11.0-py3-none-win_amd64.whl.

File metadata

  • Download URL: playwright-1.11.0-py3-none-win_amd64.whl
  • Upload date:
  • Size: 14.8 MB
  • Tags: Python 3, Windows x86-64
  • Uploaded using 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

File hashes

Hashes for playwright-1.11.0-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 6c6940e858e98997e57738e8ddbc3675bd83f2a4499975780104ba611e9d6df3
MD5 9e6ab16c9fd0fe15ebc1b7aa79ffc0d5
BLAKE2b-256 6b3069dccfd605af20fb2732a7479b4a9a370e5de93ec2f4b61b77ff82cbf45c

See more details on using hashes here.

File details

Details for the file playwright-1.11.0-py3-none-win32.whl.

File metadata

  • Download URL: playwright-1.11.0-py3-none-win32.whl
  • Upload date:
  • Size: 13.2 MB
  • Tags: Python 3, Windows x86
  • Uploaded using 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

File hashes

Hashes for playwright-1.11.0-py3-none-win32.whl
Algorithm Hash digest
SHA256 38a1f396dcd75ec8083e5d74c9ba2fd6c0808bb63cc0c86e9eba676312d1c71b
MD5 ced351ee2c67e5b385c7935eafbe04d1
BLAKE2b-256 09a0b39d854f8f3418290894881ba5d88e8f88f64fd6069cfc0e28bd51e6bc50

See more details on using hashes here.

File details

Details for the file playwright-1.11.0-py3-none-manylinux_2_27_x86_64.whl.

File metadata

  • Download URL: playwright-1.11.0-py3-none-manylinux_2_27_x86_64.whl
  • Upload date:
  • Size: 20.1 MB
  • Tags: Python 3, manylinux: glibc 2.27+ x86-64
  • Uploaded using 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

File hashes

Hashes for playwright-1.11.0-py3-none-manylinux_2_27_x86_64.whl
Algorithm Hash digest
SHA256 a41080022622612b53d97190f2eaae5dbb4e2051fdc8da6541f170fee0fa82a6
MD5 3e0579c1f70ab3de0f95e2a803784172
BLAKE2b-256 03602fe2695b4f776d4ef45814534263bbb83876aa312383ffda272528495fab

See more details on using hashes here.

File details

Details for the file playwright-1.11.0-py3-none-macosx_11_0_universal2.whl.

File metadata

  • Download URL: playwright-1.11.0-py3-none-macosx_11_0_universal2.whl
  • Upload date:
  • Size: 17.9 MB
  • Tags: Python 3, macOS 11.0+ universal2 (ARM64, x86-64)
  • Uploaded using 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

File hashes

Hashes for playwright-1.11.0-py3-none-macosx_11_0_universal2.whl
Algorithm Hash digest
SHA256 0a6e540d3a6c7810ccf2d614fd831e38412a72f331b03997e02f43cc007ea680
MD5 0c786bc069c6826907a45a55be28a4f6
BLAKE2b-256 9f37e38e0cb3281e25db263bc22cd440a971a5f141d15045ce0ca16fa7275379

See more details on using hashes here.

File details

Details for the file playwright-1.11.0-py3-none-macosx_10_13_x86_64.whl.

File metadata

  • Download URL: playwright-1.11.0-py3-none-macosx_10_13_x86_64.whl
  • Upload date:
  • Size: 17.9 MB
  • Tags: Python 3, macOS 10.13+ x86-64
  • Uploaded using 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

File hashes

Hashes for playwright-1.11.0-py3-none-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 d1073343845b55ecec2004685a646d045768b35d40cad612c779066164dcfb91
MD5 671cc2beb7d36b6d82ba00a8e55afd5f
BLAKE2b-256 b62effa3c772d31be145be606dc854dfa8f1e1e46d0d5ff4808fd5147915b80c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page