Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Playwright for Python is a browser-automation library: it lets Python code open web pages, interact with them, and inspect results. It supports Chromium, Firefox, and WebKit, and provides both synchronous and asynchronous APIs. To use it from VS Code, install the Python package in your selected Python environment, then install the matching browser binaries separately.

What Playwright does

Playwright automates browser tasks, from opening a page and reading its title to exercising a web application in an end-to-end test. Its Python library supports synchronous and asynchronous code. The supported browser engines are Chromium, Firefox, and WebKit; you can start with one and add others when you need broader browser coverage. See the Playwright Python library guide.

Before you install

  • Install Visual Studio Code and a Python version supported by the current Playwright Python installation guide. The guide currently lists Python 3.8 or higher and supported operating-system releases; check its live requirements because they can change.
  • Open your project folder in VS Code and select the Python interpreter or environment you plan to use. The package must be installed into that same environment.
  • Use the VS Code terminal for the commands below. If you use a virtual environment, activate it before installing.

Install the Playwright Python library and browsers

  1. Open the project in VS Code. Choose File > Open Folder…, then select the folder for your Python project.
  2. Select the Python environment. Use the Python interpreter selector in VS Code to choose the environment for this project. If you do not see it, open the Command Palette and run Python: Select Interpreter.
  3. Open the integrated terminal. Choose Terminal > New Terminal. Confirm that the selected environment is active. If pip needs updating, run python -m pip install --upgrade pip.
  4. Install the Python package. In that terminal, run pip install playwright. This installs the library, not the browser binaries.
  5. Download the browser binaries. Run playwright install in the same environment. Playwright needs browser binaries that correspond to its version; after upgrading the package, run the install command again if required. Downloads can use substantial disk space, and the amount varies by environment. See the Playwright browser guide.
  6. Create and run a Python file. Add the example below to a file such as hello_playwright.py, then run it from the integrated terminal with python hello_playwright.py.
from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch()
    page = browser.new_page()
    page.goto("https://playwright.dev/")
    print(page.title())
    browser.close()

The script launches Chromium, opens the Playwright site, prints the page title, and closes the browser. VS Code can run and debug Python code; Playwright’s Python documentation describes using the Visual Studio Code Python debugger.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose the right Python setup for your task

General browser automation

For scripts and other general automation, install playwright as shown above and use its sync or async API. The example uses the synchronous API; asynchronous code is another option documented in the library guide.

End-to-end tests with pytest

If you want to write tests with pytest, use Playwright’s pytest integration rather than treating the basic library install as the complete test setup. Install it with pip install pytest-playwright, then run playwright install. Create test files with names that follow pytest’s test_ convention. The Python test-running and debugging guide explains how to run and debug them.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Python Playwright and the VS Code extension are different workflows

The Python library setup uses Python packages and the playwright install command. Playwright also documents a separate VS Code extension workflow: install the extension from the Extensions view, then run Test: Install Playwright from the Command Palette. That guide lists Node.js and VS Code as prerequisites and describes the extension’s Playwright Test workflow. It is not a substitute for installing the Python package or its browser binaries. Follow the VS Code extension guide if that is the workflow you intend to use.

Common setup problems

  • Python cannot import Playwright: the package may have been installed in a different environment. Select the intended interpreter in VS Code and install the package from that environment’s terminal.
  • The browser does not launch: check that you ran playwright install after installing or upgrading the package. The browser binaries need to match the Playwright version.
  • You expected the extension command to install Python Playwright: the extension and the Python library have separate setup paths. Use the Python package and browser installation commands for Python code.
  • You need pytest commands or test discovery: install pytest-playwright and use the Python testing documentation rather than relying only on the general-purpose library setup.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.