Playwright with Python: install it and write your first test
Answer first
To start with Playwright in Python, install the pytest plugin with pip install pytest-playwright, download the browsers with playwright install, write a test function that takes the page fixture, and run pytest. Tests run headless in Chromium by default; --headed and --browser change that.
- Published
- Updated
- min
- 4
Playwright for Python is the Python version of the Playwright browser automation library, and pytest-playwright is its official plugin for pytest. This guide installs both, writes one test, runs it in three browsers and shows where the output goes.
What do I need before installing Playwright for Python?
You need a supported Python and a supported operating system. The playwright package on PyPI, version 1.65.2 on the day we checked, declares Requires-Python >=3.10 (PyPI: playwright, read 2026-11-26). Playwright's Python introduction lists these systems (Playwright Python installation, read 2026-11-26):
- Windows 11+, Windows Server 2019+ or Windows Subsystem for Linux (WSL)
- macOS 14 (Sonoma) or later
- Debian 12 / 13, Ubuntu 22.04 / 24.04 / 26.04 (x86-64 or arm64)
The same page still says "Python 3.8 or higher". The package metadata is newer, so treat 3.10 as the floor.
How do I install Playwright and pytest-playwright?
Install the plugin, which pulls in Playwright, then download the browsers.
- Create and activate a virtual environment:
python -m venv .venv
source .venv/bin/activate
- Install the plugin with your package manager. The official commands are
pip install pytest-playwright,poetry add pytest-playwrightoruv add pytest-playwright(Playwright Python installation, read 2026-11-26).
pip install pytest-playwright
- Download the browser builds:
playwright install
Playwright says this step installs "browser binaries for Chromium, Firefox and WebKit" (Playwright Python library, read 2026-11-26). On a fresh Linux machine, playwright install --with-deps also installs the system packages the browsers need (Playwright browsers, read 2026-11-26).
How do I write my first Playwright test in Python?
Write a function whose name starts with test_ and that takes the page fixture. This is Playwright's own first example, saved as test_example.py (Playwright Python installation, read 2026-11-26):
import re
from playwright.sync_api import Page, expect
def test_has_title(page: Page):
page.goto("https://playwright.dev/")
expect(page).to_have_title(re.compile("Playwright"))
def test_get_started_link(page: Page):
page.goto("https://playwright.dev/")
page.get_by_role("link", name="Get started").click()
expect(page.get_by_role("heading", name="Installation")).to_be_visible()
We ran this file with pytest-playwright 0.10.0, playwright 1.65.2 and Chromium on 26 November 2026, and both tests passed. Three things in it do most of the work:
- The
pagefixture. The plugin hands each test a "New browser page for a test", inside a "New browser context for a test" (Playwright Python test runners, read 2026-11-26). No cookies or storage carry over between tests. - Locators by role.
get_by_role("link", name="Get started")finds the element the way a user would, which survives most markup changes. expect. Playwright's assertions wait and retry until the condition holds or the timeout passes, so the test needs nosleep.
How do I run the test?
Run pytest in the project folder. Playwright notes that "By default tests will be run on chromium" and that "Tests are run in headless mode meaning no browser UI will open up when running the tests" (Playwright Python installation, read 2026-11-26).
The commands you will use most, from Playwright Python: running tests (read 2026-11-26):
Table: Common pytest commands for Playwright
| Task | Command |
|---|---|
| Run every test | pytest |
| Show the browser | pytest --headed |
| Run in WebKit | pytest --browser webkit |
| Run in two browsers | pytest --browser webkit --browser firefox |
| Run one file | pytest test_login.py |
| Run tests whose name matches | pytest -k test_add_a_todo_item |
| Step through with the Inspector | PWDEBUG=1 pytest -s |
Which pytest-playwright options should I know?
These plugin options cover most day-to-day needs. Each description is the plugin's own (Playwright Python test runners, read 2026-11-26):
Table: pytest-playwright command line options and their defaults
| Option | What it does | Default |
|---|---|---|
--headed | Run tests in headed mode | headless |
--browser | chromium, firefox or webkit; may be repeated | chromium |
--browser-channel | Browser channel to use, such as chrome | not set |
--slowmo | Slow each operation by this many milliseconds | not set |
--device | Device to emulate | not set |
--output | Directory for artifacts produced by tests | test-results |
--tracing | on, off or retain-on-failure | off |
--video | on, off or retain-on-failure | off |
--screenshot | on, off or only-on-failure | off |
A useful default for a shared test run is --tracing retain-on-failure: a trace is kept only for the tests that failed, and you open it with playwright show-trace followed by the trace file's path.
Which fixtures does the plugin provide?
The plugin provides fixtures at two scopes. Per test (function scope): context, page and new_context, the last for tests that need several users at once. Per session: playwright, browser_type, browser, browser_name, browser_channel and the booleans is_chromium, is_webkit and is_firefox (Playwright Python test runners, read 2026-11-26).
The split matters for speed and isolation: the browser starts once per session, while every test gets a new context, which is cheap to create and shares nothing with the previous test.
How do I keep Playwright up to date?
Upgrade both packages together, then download the matching browsers. The official pip command is pip install pytest-playwright playwright -U (Playwright Python installation, read 2026-11-26), followed by:
playwright install
Each Playwright release expects specific browser builds: "Each version of Playwright needs specific versions of browser binaries to operate" (Playwright browsers, read 2026-11-26). Skip the second step and the upgraded library looks for browser builds that are not on the machine.
If your code uses asyncio, read our guide to the Playwright async API in Python next. To spread the suite over several processes, see running browser tests in parallel.
What we read for this article
- Playwright Python installation, read 2026-11-26
- Playwright Python library, read 2026-11-26
- Playwright Python test runners, read 2026-11-26
- Playwright Python: running tests, read 2026-11-26
- Playwright browsers, read 2026-11-26
- PyPI: playwright, read 2026-11-26
Q&A
What Python version does Playwright need?
The current playwright package on PyPI (1.65.2 when we checked on 26 November 2026) declares Python 3.10 or newer. The getting-started page still says 3.8 or higher, so go by the package's own requirement.
Do I need pytest to use Playwright in Python?
No. The playwright package works as a plain library with a sync or async API. The pytest plugin, pytest-playwright, is the recommended way to write end-to-end tests because it gives each test a fresh page and context.
Why does my Playwright test not open a browser window?
Tests run headless by default, so no window opens. Run pytest --headed to see the browser.
How do I run Playwright tests in Firefox or WebKit from Python?
Pass --browser firefox or --browser webkit to pytest. The option can be given more than once to run the same tests in several browsers.
How do I update Playwright in a Python project?
Run pip install pytest-playwright playwright -U, then playwright install again so the browser builds match the new version.