Skip to main content

Getting started - Library

Installation​

PyPI version

pip install --upgrade pip
pip install playwright
playwright install

These commands download the Playwright package and install browser binaries for Chromium, Firefox and WebKit. To modify this behavior see installation parameters.

Usage​

Once installed, you can import Playwright in a Python script, and launch any of the 3 browsers (chromium, firefox and webkit).

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page()
page.goto("https://playwright.dev")
print(page.title())
browser.close()

Playwright supports two variations of the API: synchronous and asynchronous. If your modern project uses asyncio, you should use async API:

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()
await page.goto("https://playwright.dev")
print(await page.title())
await browser.close()

asyncio.run(main())

First script​

In our first script, we will navigate to https://playwright.dev/ and take a screenshot in WebKit.

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
browser = p.webkit.launch()
page = browser.new_page()
page.goto("https://playwright.dev/")
page.screenshot(path="example.png")
browser.close()

By default, Playwright runs the browsers in headless mode. To see the browser UI, set headless option to False. You can also use slow_mo to slow down execution. Learn more in the debugging tools section.

firefox.launch(headless=False, slow_mo=50)

Interactive mode (REPL)​

You can launch the interactive python REPL:

python

and then launch Playwright within it for quick experimentation:

from playwright.sync_api import sync_playwright
playwright = sync_playwright