Playwright Python Get Started
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 pageplaywright, the Playwright library itselfpytest, 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.