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 90.0.4421.0
WebKit 14.1
Firefox 86.0b10

Headless execution is supported for all browsers on all platforms.

Usage

pip install playwright==1.8.0a1
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 you app is based on the modern asyncio loop and you are used to async/await constructs, Playwright exposes Async API for you. You should also use this API inside the Jupyter Notebook and other REPL frameworks that are already based on 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 Jupyter Notebook:

>>> 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!

Is Playwright ready?

Yes, Playwright for Python is ready! The latest version of Playwright for Python is 1.8.0a. We are ready to drop the Alpha bit once we hear from you. Once it is gone, we will become semver compatible and the API will be frozen in its present form for years. We will still be adding features with every release, but we promise to not break it anymore!

Migration from the pre-release versions

The API has changed since the last 0.170.0 version:

  • Snake case notation for methods and arguments:

    # old
    browser.newPage()
    
    # new
    browser.new_page()
    
  • Import has changed to include sync vs async mode explicitly:

    # old
    from playwright import sync_playwright
    
    # new
    from playwright.sync_api import sync_playwright
    

That's about it! Our new doc site uses proper notation and examples for the new API.

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.9.1-py3-none-win_amd64.whl (39.9 MB view details)

Uploaded Python 3 Windows x86-64

playwright-1.9.1-py3-none-win32.whl (34.9 MB view details)

Uploaded Python 3 Windows x86

playwright-1.9.1-py3-none-manylinux1_x86_64.whl (58.6 MB view details)

Uploaded Python 3

playwright-1.9.1-py3-none-macosx_11_0_universal2.whl (57.1 MB view details)

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

playwright-1.9.1-py3-none-macosx_10_13_x86_64.whl (57.1 MB view details)

Uploaded Python 3 macOS 10.13+ x86-64

File details

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

File metadata

  • Download URL: playwright-1.9.1-py3-none-win_amd64.whl
  • Upload date:
  • Size: 39.9 MB
  • Tags: Python 3, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.7.0 requests/2.25.1 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.8.7

File hashes

Hashes for playwright-1.9.1-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 aa084cab5b80f4b8bd52df3334a96f3d8f1f376a10fca7e46003c027eafcf1cf
MD5 1a736e696d383401c2b9d1bfaa6f5054
BLAKE2b-256 e340d00479d326a7f3bcf068a06934da13619aa1e2575194a45d36ee0fe8f869

See more details on using hashes here.

File details

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

File metadata

  • Download URL: playwright-1.9.1-py3-none-win32.whl
  • Upload date:
  • Size: 34.9 MB
  • Tags: Python 3, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.7.0 requests/2.25.1 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.8.7

File hashes

Hashes for playwright-1.9.1-py3-none-win32.whl
Algorithm Hash digest
SHA256 6600c1ccde3c3e57ddfd27b1581fed11ed29485ae4c3d9adda160a0dc6d288e7
MD5 8dd3f1035b42f481f86706e520fcc04d
BLAKE2b-256 17b948c68f4c30f6472da15bf69b30ddcaf2266510a4236f7efc1ba65e2dfa02

See more details on using hashes here.

File details

Details for the file playwright-1.9.1-py3-none-manylinux1_x86_64.whl.

File metadata

  • Download URL: playwright-1.9.1-py3-none-manylinux1_x86_64.whl
  • Upload date:
  • Size: 58.6 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.7.0 requests/2.25.1 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.8.7

File hashes

Hashes for playwright-1.9.1-py3-none-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 13eda4b7c89fdd4357fc809dde96089601f336fe036326ee3dfcdd0012d24b1c
MD5 b1afd13464e922ac1663991de888db1a
BLAKE2b-256 1fe639c6ce7268ab56743ed60e63dfcfbaf6a9bb485ddebbdc17ba114523ba47

See more details on using hashes here.

File details

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

File metadata

  • Download URL: playwright-1.9.1-py3-none-macosx_11_0_universal2.whl
  • Upload date:
  • Size: 57.1 MB
  • Tags: Python 3, macOS 11.0+ universal2 (ARM64, x86-64)
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.7.0 requests/2.25.1 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.8.7

File hashes

Hashes for playwright-1.9.1-py3-none-macosx_11_0_universal2.whl
Algorithm Hash digest
SHA256 8b64734184e1d1faee74be55919fe72950a1a1f7ca59635e75881af02b52d10f
MD5 b9cbcdf950be1fb684b51b82ff3509c7
BLAKE2b-256 a62b70c4352013a556e1e042c3d4f6b919117fa72cef1e2ab3ad8205d4aeae65

See more details on using hashes here.

File details

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

File metadata

  • Download URL: playwright-1.9.1-py3-none-macosx_10_13_x86_64.whl
  • Upload date:
  • Size: 57.1 MB
  • Tags: Python 3, macOS 10.13+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.7.0 requests/2.25.1 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.8.7

File hashes

Hashes for playwright-1.9.1-py3-none-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 920aca579760a8108cee321a1aad08f521daedfb5d40badc8b29c4df4a67460f
MD5 fe4dc24a775a14729473fa9c150589a4
BLAKE2b-256 b8622a50d43f10ca63fb7c3938d106175cf652b1cfd98fcf6eb129ded473f95b

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