Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST TOOLS MATH

Playwright Python Get Started


Share

Playwright with Python

Playwright for Python is the same browser automation tool as Playwright for JavaScript, used from Python code.

It controls the same browsers, finds elements the same way, and waits for the page the same way.

Only the language around it changes.

In this chapter you install Playwright for Python and set up a test project.

The next chapters show how to write and run tests, and how the Python code compares to the JavaScript code in the rest of this tutorial.


Requirements

You need Python installed on your computer.

Playwright 1.60 for Python needs Python 3.9 or newer.

Newer Playwright releases can raise this minimum, so check the requirement on the Playwright page on pypi.org before you install.

Check your Python version in a terminal:

Example

python --version

If you do not have Python, read our Python Get Started chapter.

Note: The examples in this chapter were tested with Playwright 1.60 and pytest-playwright 0.7.1.



Create a Virtual Environment

A virtual environment is a folder that holds the Python packages for one project.

It keeps Playwright and its helpers apart from the packages of your other projects.

Create an empty folder for your project, open a terminal in it, and run:

Example

python -m venv venv

This creates a virtual environment in a folder named venv.

Activate it before you install anything.

On Windows:

Example

venv\Scripts\activate

On macOS and Linux:

Example

source venv/bin/activate

The name of the environment now shows in front of your terminal prompt.

Activate it again every time you open a new terminal for this project.

Read more in our Python Virtual Environment chapter.


Install Playwright

The easiest way to write Playwright tests in Python is with pytest, the most used test runner for Python.

A test runner is the program that finds your tests, runs them and shows the results.

Install the Playwright plugin (an add-on) for pytest with pip:

Example

pip install pytest-playwright

This one command installs three main packages:

  • pytest-playwright, the plugin that gives your tests a ready browser page
  • playwright, the Playwright library itself
  • pytest, the test runner

Then download the browsers that Playwright controls:

Example

playwright install

This downloads Chromium, Firefox and WebKit.

To download only one browser, name it:

Example

playwright install chromium

Note: On Linux, the browsers may need extra system libraries.

Run playwright install --with-deps to install them together with the browsers.


Project Layout

A pytest project needs no configuration file to start.

pytest looks for test files by their name: files named test_*.py or *_test.py.

Inside a test file, every function whose name starts with test is a test.

A typical project looks like this:

File Description
venv/ The virtual environment, with Playwright and pytest installed
tests/test_todo.py A test file. You can have as many as you like
tests/conftest.py Optional. Shared fixtures (helpers that tests can ask for, like page) and settings for the tests in this folder
pytest.ini Optional. Command line options you want on every run

Create the tests folder now.

You will write the first test in it in the next chapter.


Check the Version

To see which version of Playwright is installed, run:

Example

playwright --version

Output:

Version 1.60.0

If your terminal does not find the playwright command, make sure the virtual environment is active.

You can also run it through Python: python -m playwright --version.

Tip: Run playwright install again after you upgrade Playwright with pip install -U pytest-playwright playwright.

Each Playwright version needs its own browser versions.



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.