Skip to content

How to Set Up Playwright in Python

Using Playwright in a Python project

When it comes to browser automation and testing, Playwright is one of the most powerful and versatile tools. Originally a JavaScript library, Playwright now offers support for Python. This allows developers to take advantage of its end-to-end testing and browser automation capabilities within the Python ecosystem.

In this article, you will learn how to get started with Playwright in Python and start automating browser tasks.

What Is Playwright?

The Playwright logo
The Playwright logo

Playwright is a cross-browser automation library that enables developers to automate browser actions such as clicking buttons, filling forms, and navigating through web pages. It provides a unified API to interact with multiple browsers, including Chrome, Firefox, and WebKit.

With Playwright, you can write browser automation scripts that are reliable, fast, and easy to maintain.

Is Playwright Available in Python?

Yes! While Playwright is originally a JavaScript library, it has expanded its support to other programming languages, including Python. This means that you can leverage the power of Playwright directly in your Python projects, enjoying the same capabilities and flexibility as its JavaScript counterpart.

Prerequisites

Before diving into how to use Playwright with Python, make sure you have the following prerequisites in place:

Initialize a Playwright Project in Python

Follow this step-by-step section to create a Playwright project in Python.

1. Set Up a Python Project

In the terminal, create the project folder with:

mkdir playright-python-demo

Enter it and initialize a new virtual environment with:

cd playright-python-demo
python -m venv env

The playright-python-demo project directory should now contain a venv folder.

2. Install Playwright

As explained in the official documentation, the easiest way to get started with Playwright in Python is through its Pytest plugin. If you are not familiar with Pytest, this is a popular testing framework for Python. It provides a simple and scalable way to write unit, functional, and integration tests in Python.

In the project directory, launch the command below to install the Playwright Pytest plugin:

pip install pytest-playwright

Then, install the controllable browsers with:

playwright install

This will download and set up the browser binaries for Chrome, Firefox, and WebKit.

3. Create Your First Test

In the project folder, create a test.py file and initialize it as below:

# test.py

import re
from playwright.sync_api import Page, expect

def test_homepage_has_Playwright(page: Page):
    # connect to the target page
    page.goto("https://playwright.dev/")

    # expect the page title "to contain" a substring.
    expect(page).to_have_title(re.compile("Playwright"))

This script simply visits the Playwright home page and verifies that the page title contains the “Playwright” string. Note that the to_have_title() accepts a Python regex. Here is why the re.compile() instruction.

4. Run the Test

Launch your sample test with:

pytest

By default, the tests will be executed on Chrome and in headless mode, meaning that no browser will be shown during the execution. This behavior can be configured via the CLI arguments.

The results of the tests and the logs will be shown in the terminal.

Et voilà! You know now how to use Playwright in Python.

Conclusion

Setting up Playwright in Python opens up a world of possibilities for browser automation and testing. By following this step-by-step tutorial, you saw how to initialize a Playwright project in Python. Explore the Playwright documentation and unleash the full potential of this powerful tool in your Python projects.

Thanks for reading! I hope you found this article helpful.

nv-author-image

Antonello Zanini

I'm a software engineer, but I prefer to call myself a Technology Bishop. Spreading knowledge through writing is my mission.View Author posts

Want technical content like this in your blog?