Get started with Playwright for Python
Last modified on Tue 25 Apr 2023

What is Playwright?

Playwright is a test automation tool for Web testing. It supports testing on various browsers in both headed and headless mode. You can write tests in JavaScript/TypeScript, C#, Java and Python. Depending on your language of choice, the setup slightly differs.

This is a short and simple guide to quickly get started with Playwright for Python.



  1. Create a new PyCharm project
  2. Install pytest-playwright plugin
    • pip install pytest-playwright
  3. Install browsers
    • playwright install
  4. Create a test file within the current working directory or a sub-directory
    • File name e.g.:
    • See example test below
  5. Run the test
    • On default (headless) browser:
      • python3 -m pytest
    • On specific (headed) browser:
      • python3 -m pytest --browser firefox --headed


import re

from playwright.sync_api import Page, expect

def test_homepage(page: Page):

   # Expect a title "to contain" a substring

   # Create a locator for button
   accept_all_button = page.get_by_role("button", name="Accept all")

   # Take a screenshot

   # Click the button

   # Expect the button to be hidden

   # Take a full page screenshot (screenshot of a full scrollable page)
   page.screenshot(path="screenshot_full_page.png", full_page=True)