Last modified: Jun 06, 2026

Install Playwright in Python Guide

Playwright is a powerful tool for browser automation. It works with Chromium, Firefox, and WebKit. This guide shows you how to install Playwright in Python quickly.

You will learn the steps from setup to running your first test. The process is simple and works on Windows, macOS, and Linux.

What is Playwright?

Playwright lets you control browsers programmatically. You can click buttons, fill forms, and take screenshots. It is faster and more reliable than older tools like Selenium.

Microsoft created Playwright. It supports modern web features and auto-waits for elements. This makes your tests more stable.

Prerequisites

Before you start, make sure you have Python installed. You need Python 3.7 or higher. Check your version with this command:

python --version

You should see output like:

Python 3.9.10

If you don't have Python, download it from the official website. You also need pip, the Python package manager. It usually comes with Python.

Step 1: Create a Virtual Environment

It is good practice to use a virtual environment. This keeps your project dependencies separate. Run these commands:

python -m venv playwright_env
source playwright_env/bin/activate  # On Linux/macOS
# Or on Windows:
playwright_env\Scripts\activate

Your terminal prompt will change. This means the virtual environment is active. Now you can install packages without affecting other projects.

Step 2: Install Playwright

Use pip to install Playwright. The package name is playwright. Run this command:

pip install playwright

This installs the core library. It downloads about 10 MB of data. Wait for the installation to finish.

If you see errors, upgrade pip first:

pip install --upgrade pip

Then try again. The installation should complete without issues.

Step 3: Install Browsers

Playwright needs browser binaries. Run this command to download them:

playwright install

This command downloads Chromium, Firefox, and WebKit. It takes a few minutes. The total size is about 400 MB.

You can install only one browser if you prefer:

playwright install chromium

This saves disk space. For most tests, Chromium is enough.

Step 4: Verify the Installation

Create a simple script to check everything works. Make a file called test_playwright.py:

# Import the Playwright module
from playwright.sync_api import sync_playwright

# Start Playwright
with sync_playwright() as p:
    # Launch a Chromium browser
    browser = p.chromium.launch(headless=False)
    # Open a new page
    page = browser.new_page()
    # Navigate to a website
    page.goto("https://example.com")
    # Print the page title
    print(page.title())
    # Close the browser
    browser.close()

Run the script:

python test_playwright.py

You should see a browser window open. It will go to example.com. The output will be:

Example Domain

This means Playwright works correctly. The browser closes automatically after the script ends.

Common Installation Issues

Sometimes you may face problems. Here are solutions for common errors.

Error: 'playwright' command not found

This happens if the virtual environment is not active. Make sure you see the environment name in your terminal prompt. Activate it again if needed.

Error: Failed to install browsers

This often occurs due to network issues. Try running the install command again. You can also set a proxy if you are behind a firewall.

playwright install --with-deps

The --with-deps flag installs system dependencies. This helps on Linux systems.

Error: ModuleNotFoundError: No module named 'playwright'

This means Playwright is not installed in your current environment. Double-check you are in the right virtual environment. Run pip list to see installed packages.

Using Playwright in Headless Mode

For automated testing, headless mode is faster. It runs the browser without a GUI. Change the launch method:

# Launch browser in headless mode
browser = p.chromium.launch(headless=True)

Headless mode is perfect for CI/CD pipelines. It uses less memory and runs faster.

Installing Playwright with Specific Browsers

You can install only the browsers you need. Use the --browser flag:

playwright install --browser=firefox

This installs only Firefox. You can also install multiple browsers:

playwright install chromium firefox

This saves time and disk space. Choose based on your testing needs.

Writing Your First Test

Let's write a complete test. This script searches on Google:

# Import Playwright
from playwright.sync_api import sync_playwright

# Start Playwright context
with sync_playwright() as p:
    # Launch browser
    browser = p.chromium.launch(headless=False)
    # Create a new page
    page = browser.new_page()
    # Go to Google
    page.goto("https://www.google.com")
    # Type in search box
    page.fill("input[name='q']", "Playwright Python")
    # Press Enter
    page.press("input[name='q']", "Enter")
    # Wait for results
    page.wait_for_selector("h3")
    # Get first result title
    first_result = page.query_selector("h3")
    print(first_result.inner_text())
    # Close browser
    browser.close()

Run this script. It will open Google, search for "Playwright Python", and print the first result title. This shows you the basic workflow.

Conclusion

Installing Playwright in Python is straightforward. You create a virtual environment, install the package, and download browsers. The whole process takes less than 10 minutes.

Playwright gives you reliable browser automation. It works across all major browsers. You can use it for testing, scraping, or monitoring websites.

Start with simple scripts. Then explore advanced features like network interception and mobile emulation. Playwright has excellent documentation to help you.

Now you have a working Playwright setup. Go ahead and automate your browser tasks. Happy coding!