Skip
AgentSandbox.host

Browser testing at scale with Playwright and Selenium: setup in Python and Node.js, Docker images, Selenium Grid, parallel runs, sharding and flaky tests.

Home + Log

Playwright async in Python: when to use it and how

Answer first

Use Playwright's async API in Python when your code already runs on asyncio. Import async_playwright from playwright.async_api, await every call, and start it with asyncio.run. For pytest, install pytest-playwright-asyncio and mark tests with the session loop scope.

Writer
Published
Updated
min
4

Playwright for Python ships two APIs with the same methods: a synchronous one in playwright.sync_api and an asynchronous one in playwright.async_api. Playwright's own guidance is short: "If your modern project uses asyncio, you should use async API" (Playwright Python library, read 2026-11-26).

When should I use the async API instead of the sync API?

Use the async API when your program already runs an asyncio event loop, or when one process must drive several pages at once. Use the sync API for plain scripts and ordinary pytest suites.

Table: Choosing between Playwright's sync and async APIs in Python

SituationAPIWhy
A pytest suite, one page per testSyncSimplest code; the plugin gives each test its own page
A script that visits pages one by oneSyncNo event loop to manage
An asyncio web service or workerAsyncThe sync API refuses to start inside a running event loop
Many pages driven from one processAsyncWaits overlap, so total time follows the slowest page rather than the sum
Code split across threadsEither, one instance per thread"Playwright's API is not thread-safe" (Playwright Python library, read 2026-11-26)

How do I write a first async Playwright script?

Wrap the work in an async function, enter async_playwright() with async with, await every call, and start it with asyncio.run. This is the shape of Playwright's own async example (Playwright Python BrowserType API, read 2026-11-26):

import asyncio
from playwright.async_api import async_playwright, Playwright

async def run(playwright: Playwright):
    chromium = playwright.chromium
    browser = await chromium.launch()
    page = await browser.new_page()
    await page.goto("https://example.com")
    # other actions...
    await browser.close()

async def main():
    async with async_playwright() as playwright:
        await run(playwright)

asyncio.run(main())

Install it the same way as the sync API: pip install playwright, then playwright install to download the browsers (Playwright Python library, read 2026-11-26).

How do I run several pages at the same time?

Run several pages at once by creating one browser, one context per job, and gathering the jobs. asyncio.gather will "Run awaitable objects in the aws sequence concurrently" (Python asyncio tasks, read 2026-11-26), and a semaphore caps how many run together (Python asyncio synchronization primitives, read 2026-11-26).

import asyncio
from playwright.async_api import async_playwright

URLS = [
    "https://example.com",
    "https://playwright.dev",
    "https://www.python.org",
]

async def title_of(browser, url, limit):
    async with limit:
        context = await browser.new_context()
        try:
            page = await context.new_page()
            await page.goto(url)
            return url, await page.title()
        finally:
            await context.close()

async def main():
    limit = asyncio.Semaphore(4)
    async with async_playwright() as p:
        browser = await p.chromium.launch()
        results = await asyncio.gather(*(title_of(browser, u, limit) for u in URLS))
        await browser.close()
    for url, title in results:
        print(url, title)

asyncio.run(main())

We ran this script with playwright 1.65.2 and Chromium on 26 November 2026; it printed the three page titles. Each job gets its own context, so cookies and storage never mix between jobs. Playwright describes contexts as "fast and cheap to create" and "completely isolated, even when running in a single browser" (Playwright browser contexts, read 2026-11-26). The semaphore matters: every open page uses memory and CPU, and an unbounded gather over a long list opens them all at once.

How do I write async tests with pytest?

Write async tests with the separate pytest-playwright-asyncio plugin. Playwright's instructions: "To use async fixtures, install pytest-playwright-asyncio", and "Ensure you are using pytest-asyncio>=0.26.0 and set asyncio_default_test_loop_scope = session in your configuration" (Playwright Python test runners, read 2026-11-26).

  1. Install the plugin:
pip install pytest-playwright-asyncio
playwright install
  1. Set the loop scope in pyproject.toml:
[tool.pytest.ini_options]
asyncio_default_test_loop_scope = "session"
  1. Write the test with the session loop scope, as in Playwright's example (Playwright Python test runners, read 2026-11-26):
import pytest
from playwright.async_api import Page

@pytest.mark.asyncio(loop_scope="session")
async def test_foo(page: Page):
    await page.goto("https://github.com")
    # ...

The session loop scope matters because the browser fixture lives for the whole session. A test that ran on its own event loop could not use a browser created on another loop.

Version 0.10.0 of pytest-playwright-asyncio, current on PyPI when we checked, requires Python 3.10 or newer and pytest 8.2.0 or newer (PyPI: pytest-playwright-asyncio, read 2026-11-26).

What goes wrong most often with async Playwright?

Four mistakes cause most async Playwright errors:

  1. A missing await. The call returns a coroutine and nothing happens. Assertions then pass or fail for the wrong reason.
  2. Calling the sync API inside an event loop. We ran sync_playwright() inside asyncio.run with playwright 1.65.2 on 26 November 2026, and it stopped with "It looks like you are using Playwright Sync API inside the asyncio loop. Please use the Async API instead." Inside a running loop, such as a notebook or an async web framework, use playwright.async_api.
  3. Changing the Windows event loop. Playwright "requires ProactorEventLoop of asyncio" on Windows, because the selector loop "does not supports async subprocesses" (Playwright Python library, read 2026-11-26).
  4. Sharing one instance across threads. Create "a playwright instance per thread" if you must use threads (Playwright Python library, read 2026-11-26).

New to Playwright in Python? Start with our Playwright Python setup guide.

What we read for this article

Q&A

  1. Is Playwright's async API faster than the sync API in Python?

    Not for one page. Both drive the same browser. Async helps when one process must drive several pages or browsers at the same time, because the waits overlap instead of running one after another.

  2. Can I use Playwright from several threads?

    Playwright's API is not thread-safe. Its documentation says to create a Playwright instance per thread if you use threads. With asyncio you can run many pages concurrently in a single thread instead.

  3. Why does Playwright fail with NotImplementedError on Windows?

    Playwright needs asyncio's ProactorEventLoop on Windows, because the SelectorEventLoop does not support async subprocesses. Do not switch the event loop policy to the selector loop in code that starts Playwright.

  4. How do I write async Playwright tests with pytest?

    Install pytest-playwright-asyncio, use pytest-asyncio 0.26.0 or newer, set asyncio_default_test_loop_scope = session in your pytest configuration, and decorate each test with @pytest.mark.asyncio(loop_scope="session").