What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The quickest way to start end-to-end testing with Playwright for Python is to install its pytest plugin, install the browser binaries, write a test that uses the supplied page fixture, and run pytest. For a standalone automation script rather than a test suite, install the playwright package and use its synchronous or asynchronous API directly.

Choose the right Python workflow

Playwright for Python has two common starting points. The pytest plugin is designed for repeatable end-to-end tests: pytest discovers tests, and the plugin provides browser fixtures and Playwright’s web-first assertions. The direct library API is a natural fit for one-off scripts or automation that is not organized as a pytest test suite.

Approach Install Use it when
pytest plugin pytest-playwright You want test discovery, fixtures, assertions, and a repeatable test suite.
Direct library playwright You want to write a standalone browser automation script.

Both approaches support Chromium, Firefox, and WebKit. For a direct script, choose the synchronous API for straightforward sequential code; use the asynchronous API when your project already uses asyncio. Neither style is universally best.

Install Playwright and its browsers

Installing the Python package and installing browser binaries are separate steps. Open a terminal in the Python environment where you plan to work and install the pytest plugin:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
pip install pytest-playwright
playwright install

The first command installs the plugin and its dependencies. The second downloads the browsers Playwright uses. The first run may take longer while those binaries are downloaded. If you are writing a standalone script instead, install the library package:

pip install playwright
playwright install

Use one workflow or the other according to your project; the browser-install step is needed either way. The installer can target a specific browser, for example playwright install webkit. Playwright’s browser guide also documents operating-system dependency installation, including playwright install-deps and playwright install --with-deps chromium. Which dependencies or system packages are needed depends on the operating system and environment. Check the current official installation documentation for supported Python and operating-system versions before setting up a machine, especially in CI or a container.

Write and run your first pytest test

Create test_example.py in your project. This example uses the plugin’s page fixture, checks the page title, follows a link by its accessible role and name, and verifies that the destination heading is visible:

from playwright.sync_api import expect


def test_get_started_link(page):
    page.goto("https://playwright.dev/")

    expect(page).to_have_title("Playwright")
    page.get_by_role("link", name="Get started").click()
    expect(
        page.get_by_role("heading", name="Installation")
    ).to_be_visible()

Run it from the directory containing the file:

pytest

By default, the pytest plugin runs tests headlessly in Chromium. Pytest discovers files and functions named with the test_ convention, so a name such as test_example.py and a function beginning test_ make the example discoverable. The plugin’s fixtures give each test a browser page and support isolated browser contexts. You can also configure the plugin for multiple browsers.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use a standalone Python script instead

For an automation script that does not need pytest discovery or test fixtures, save this as example.py after installing playwright and its browsers:

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()

The browser is explicitly closed when the script finishes. Playwright also offers an asynchronous API. Use it inside an asyncio coroutine when the surrounding application is already asynchronous:

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())

The async version awaits browser operations and uses the asynchronous Playwright import. Do not mix synchronous calls into an async flow; keep the API style consistent through the script.

Find elements with resilient locators

Playwright locators describe how to find an element and are central to its auto-waiting and retry behavior. Prefer locators that reflect what a user sees or how assistive technology identifies a control:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • page.get_by_role("button", name="Save") finds a button by role and accessible name.
  • page.get_by_text("Welcome") locates visible text.
  • page.get_by_label("Email address") targets a form control by its label.
  • page.get_by_placeholder(), page.get_by_alt_text(), and page.get_by_title() cover other user-facing attributes.
  • Use configured test IDs when the application provides them specifically for testing.

CSS selectors and XPath are available when appropriate, but semantic locators are a useful first choice when they identify the intended control. They make a test’s purpose easier to understand and avoid tying it unnecessarily to page structure.

Let Playwright wait for the page state you need

Before a locator action such as click(), Playwright checks that the locator identifies exactly one element and that it is visible, stable, enabled, and able to receive events. If those checks do not pass before the action timeout, the action fails rather than clicking an unsuitable target. Web-first assertions such as expect(locator).to_be_visible() retry until the condition succeeds or the assertion times out.

For ordinary interactions, use locator actions and assertions instead of inserting fixed sleeps. A fixed delay can make a test wait longer than necessary and does not prove the page has reached the state the next step needs. If a workflow really depends on a specific page condition, wait for that condition, such as a locator becoming visible, rather than guessing how many seconds the page will take.

Run tests in other browsers and debug failures

To see the browser while tests run, use headed mode. The plugin’s command-line options include browser selection, device emulation, and output artifacts:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
pytest --headed
pytest --browser chromium --browser firefox --browser webkit
pytest --device="iPhone 13"

Browser selections can be repeated to run the suite against multiple supported engines. The plugin also documents --browser-channel for selecting a browser channel and options for output, tracing, video, and screenshots. These CLI settings apply to the plugin’s default browser, context, and page fixtures. Consult the current plugin reference for exact artifact-option names and supported device or channel values.

For interactive debugging, the official guide documents this command:

PWDEBUG=1 pytest -s -k test_get_started_link

It opens a browser and Playwright Inspector for the selected test. The -k option filters pytest tests by name; replace test_get_started_link with a substring matching your test. Python developers can also debug with their usual debugger, including the VS Code Python extension.

Troubleshoot common setup problems

  • The browser executable is missing. Installing the package does not automatically guarantee the required browser binaries are present. Run playwright install in the same Python environment, or install the particular browser with a command such as playwright install webkit.
  • A Playwright update is followed by launch errors. Playwright releases use specific browser binary versions. Run playwright install again after updating the package so the installed browsers match the Playwright version.
  • A browser will not start on a Linux host. The host may be missing operating-system dependencies. Use the browser guide’s dependency-install commands, such as playwright install-deps or playwright install --with-deps chromium, as appropriate for the environment.
  • pytest reports that it found no tests. Check that the file is named with the test_ prefix, that the test function also starts with test_, and that you ran pytest from the project directory.
  • A click times out or an assertion fails. Check that the locator identifies the intended element and that the expected page state can actually occur. A click requires a unique, visible, stable, enabled element that can receive events; use Inspector or a headed run to inspect the page.
  • The browser is not visible during a test. Headless Chromium is the pytest default. Add --headed when you want to watch the test run.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or skip the browser setup

If your goal is to get a screenshot rather than build browser automation, ScreenshotNeo provides a website screenshot API and MCP server. This one GET request returns an image or PDF; see the ScreenshotNeo API documentation for request options.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://playwright.dev/ -o shot.webp

ScreenshotNeo accepts cookie or consent banners as a visitor and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; each cleanup step can be turned off. Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed, and response headers indicate the page verdict and billing status. An MCP server provides take_screenshot, get_page_info, and capture_pdf for Claude, Cursor, and other MCP clients. The free plan includes 1,000 screenshots a month without a card; paid plans start at $5 for 3,000 screenshots. Sign up for the free plan.

Plan for browser versions and repeatable runs

Playwright controls specific browser binary versions rather than relying on whatever browser happens to be installed on a machine. That keeps its automation paired with the browser version expected by the Playwright release, but it also means a package update can require downloading updated binaries. In a team or CI environment, install the package and browsers as part of environment setup, and make the setup repeatable rather than assuming a developer’s local browser installation will be available.

Start with default Chromium to get one test running, then add Firefox or WebKit when your test coverage requires those engines. Playwright can also work with branded Chrome or Edge channels, but those browsers are not installed by default; select a channel when that is part of your test target. Mobile device emulation is available through the plugin’s device option. Browser coverage increases the number of runs and environments you need to maintain, so choose targets based on the compatibility you need to verify.

Frequently Asked Questions

Can I use Playwright for Python for tasks other than end-to-end tests?

Yes. The direct Playwright library API is intended for general browser automation as well as test code.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Can Playwright automate Chrome or Edge?

Playwright supports branded Chrome and Edge channels. They are not installed by default; use the channel support documented for your workflow.

Does Playwright require a particular editor?

No specific editor is required. The Python debugging guide mentions the VS Code Python extension as one option, but you can use another Python debugger.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.