If you’re looking for a modern tool to automate web browsers, Playwright Python is one of the best options available today. Developed by Microsoft, Playwright allows you to automate Chromium, Firefox, and WebKit with a single API. Unlike older tools, it is designed for modern web apps that use dynamic content, single-page applications, and advanced UI elements.
- What Playwright is and Why It’s Popular
- Benefits of Playwright with Python
- Who Should Read This
- Prerequisites
- Install Python and Set Up Environment Variables
- Setting Up VS Code for Python Development
- Installing Playwright for Python
- Writing and Running Your First Playwright Python Test
- Using Locators in Playwright Python
- Capturing Screenshots and Videos in Playwright Python
- Best Practices for Beginners
- Conclusion
What Playwright is and Why It’s Popular
Playwright is an open-source automation framework that supports multiple programming languages, including Python, JavaScript, TypeScript, Java, and .NET. Its popularity has grown quickly because it:
- Works across all major browsers (Chromium, Firefox, WebKit).
- Provides fast, reliable, and headless execution.
- Handles modern web features like iframes, popups, and shadow DOM.
- Offers built-in support for test automation with frameworks like pytest.
In short, Playwright is powerful, easy to use, and ideal for end-to-end testing.
Benefits of Playwright with Python
Using Playwright with Python brings several advantages:
- Cross-Browser Support: Run tests seamlessly on Chrome, Edge, Safari, and Firefox.
- Modern Web Testing: Easily interact with advanced UI components such as dropdowns, modals, and dynamic elements.
- Ease of Setup: Installation is straightforward with just a few pip commands.
- Python Ecosystem: Leverage Python’s simplicity and integrate with popular frameworks like pytest.
This combination makes Playwright + Python an excellent choice for testers and developers who want reliable and scalable test automation.
Who Should Read This
This tutorial is designed for:
- Beginners who are just getting started with automation testing.
- Testers/QA engineers who want to move beyond Selenium and adopt modern tools.
- Python developers who want to write browser automation scripts quickly and efficiently.
If you fall into any of these categories, this guide will help you set up Playwright step by step in your local environment.
Prerequisites
Before we jump into Playwright installation, let’s make sure you have everything ready.
Basic Knowledge
- Knowing a little bit of Python programming will be helpful, but it’s not mandatory. This tutorial is beginner-friendly and will guide you through each step.
Tools and Software Required
- Python (Latest Stable Version)
- Download from the official Python website
- Make sure you add Python to your system PATH during installation.
- Visual Studio Code (VS Code)
- A lightweight, powerful, and free editor recommended for writing Python tests.
- Available at the official VS Code website.
- Internet Connection
- Required to download Python, VS Code, Playwright, and other dependencies.
Once these prerequisites are ready, you can move on to installing Python and configuring your environment for Playwright testing.
Install Python and Set Up Environment Variables
Before working with Playwright Python, you need to install Python and make sure it’s properly configured on your system.
Step 1: Download and Install Python
- Visit the official Python website: https://www.python.org/downloads/
- Download the latest stable(3.13.7) version of Python (recommended: Python 3.10 or later).
- Run the installer and check the box “Add Python to PATH” before clicking Install Now.

Step 2: Add Python to PATH (Windows)
If you missed checking the PATH option during installation:
- Open Start Menu > Search “Environment Variables”.
- Select Edit the system environment variables.
- Under System Properties, click Environment Variables.
- Find the Path variable, click Edit, then New, and add your Python installation path (e.g., C:\Users\emma\AppData\Local\Programs\Python\Python313).

Step 3: Verify Installation
Open your terminal (Command Prompt, PowerShell, or VS Code terminal) and run:
python --version
pip --version

If Python is not recognized, try using the Python launcher command instead:
py --version
- If’ python –version’ works, the PATH was set correctly.
- If only py –version works > Python is installed, but Python might not be added to PATH. You can still continue using py to run Python commands.
Setting Up VS Code for Python Development
Visual Studio Code (VS Code) makes it easier to write, run, and debug Playwright tests in Python. You can use any other IDE as well.
Step 1: Install Visual Studio Code
- Download VS Code from https://code.visualstudio.com/
- .Install it using the default settings.

Step 2: Install Python Extension
- Open VS Code > go to the Extensions Marketplace (square icon on the left sidebar).
- Search for “Python” (by Microsoft) and install it.

- This extension adds IntelliSense, code formatting, and linting support.
Step 4: Open a Workspace/Project Folder
- Create a workspace folder, e.g., Playwright Python Demo, in the D drive.
- In VS Code, go to File > Open Folder, and select the workspace folder.
- Inside it, you’ll later create a tests/ folder to organize your test scripts.
Installing Playwright for Python
To use Playwright with Python, you only need to install one package and set up the browsers.
Step 1: Install Playwright with Pytest support
Open your VS Code terminal(View > Terminal or Ctrl + `) and run:
pip install pytest-playwright

This installs both Pytest (for running tests) and Playwright (for browser automation).
Step 2: Install Playwright browsers
Next, download the supported browsers (Chromium, Firefox, WebKit) by running:
playwright install
That’s it! You now have Playwright with all dependencies ready.
Writing and Running Your First Playwright Python Test
Now let’s create and run your first test.
Step 1: Create a test file
Inside your project folder, create a directory named tests/ and add a file called test_demo.py.

Copy the test code given below and save the file.
tests/test_demo.py
from playwright.sync_api import sync_playwright
def test_open_google():
with sync_playwright() as p:
browser = p.chromium.launch(headless=False) # Set headless=True if you don’t want browser UI
# Open browser
page = browser.new_page()
# Open URL
page.goto("https://www.google.com")
# Assert Title
assert "Google" in page.title()
browser.close()
Step 2: Run all tests
In the VS Code terminal, run:
pytest
This will automatically discover and run all tests inside the tests/ folder and show the result(pass/fail) after test execution.

Step 3: Run a specific test file
If you have multiple test files and want to run only one test file:
pytest tests/test_demo.py
Step 4: Run in Visual/Debug Mode
You can also run and debug Playwright tests visually from VS Code instead of only using the terminal. For that, you need a few configurations:
- Open the Testing (Beaker) icon in the left sidebar of VS Code.
- Click on the Configure Python Tests button.
- From the options, select Pytest.

- Then select the tests/ folder, which contains your test files.
- Once configured, VS Code will display your test files with the Run and Debug buttons.
- Click Run Test or Debug Test next to your test.
- A browser will open, and you’ll see the live execution of your Playwright test.

This mode is very useful for beginners since you can visually watch your test execution, set breakpoints, and debug step-by-step inside VS Code.
Step 5: View Results
The Testing panel shows a green check for passed tests and a red cross for failed ones. You can also expand test results for detailed error messages.
Once the test execution is complete, you can view the Playwright Python test results in VS Code, as shown in the image below.

Using Locators in Playwright Python
Locators in Playwright are the heart of test automation. They allow you to find, interact with, and validate elements on a webpage. Unlike raw CSS or XPath, Playwright locators are smart, auto-waiting, and resilient, making tests less flaky and easier to maintain.
Common Locator Strategies
Here are the most widely used locator types:
1. Locator by Text
Used to find elements based on their visible text.
page.get_by_text(“Sign In”).click()
2. Locator by Role
Leverages ARIA roles for accessibility-aware testing.
page.get_by_role(“button”, name=”Submit”).click()
3. Locator by Label
Targets input fields associated with a .
page.get_by_label(“Email”).fill(“user@example.com”)
4. Locator by Placeholder
Finds input fields by their placeholder attribute.
page.get_by_placeholder(“Search…”).fill(“Playwright Python”)
5. Locator by Alt Text
Useful for images or elements with the alt attribute.
page.get_by_alt_text(“Company Logo”).click()
6. Locator by Title
Selects elements with a title attribute.
page.get_by_title(“Close”).click()
7. Locator by Test ID
Best practice for automation, relying on a data-testid attribute.
page.get_by_test_id(“login-button”).click()
8. Traditional Selectors (CSS & XPath)
If needed, Playwright also supports classic locators:
CSS Selector:
page.locator(“input[name=’username’]”).fill(“myuser”)
XPath Selector:
page.locator(“//button[@id=’login’]”).click()
Capturing Screenshots and Videos in Playwright Python
Playwright makes it simple to capture screenshots and videos of your test execution. These are extremely useful for debugging failed tests or for documenting test runs.
Capturing Screenshots
You can save screenshots at any point during your test using the page.screenshot() method. For example:
page.screenshot(path=”screenshots/google_home.png”, full_page=True)
- path: defines where the screenshot will be stored.
- full_page=True: captures the entire page instead of just the visible viewport.
Screenshots are stored in the location you specify (e.g., screenshots/ folder).
Capturing Videos
You can also record test execution by enabling video recording in the browser context.
from playwright.sync_api import sync_playwright
def test_open_google():
with sync_playwright() as p:
browser = p.chromium.launch(headless=False)
# Enable video recording
context = browser.new_context(record_video_dir="videos/")
page = context.new_page()
page.goto("https://www.google.com")
# Capture screenshot
page.screenshot(path="screenshots/google_home.png", full_page=True)
assert "Google" in page.title()
# Closing context saves the video
context.close()
browser.close()
- Videos are saved automatically in the folder you provide (e.g., videos/).
- Playwright creates subfolders for each test run, and the videos are stored in .webm format.
Where Files Are Stored
- Screenshots: in the path you define, e.g., screenshots/google_home.png.
- Videos: inside the directory you pass in record_video_dir.

Best Practices for Beginners
When starting with Playwright Python, following best practices helps keep your tests organized, maintainable, and easy to debug.
- Keep tests short and modular: Each test should focus on a single functionality. This makes debugging easier when a test fails.
- Organize tests in a tests/ folder: Separating test files from other code improves project structure and readability.
- Always use virtual environments: Avoid dependency conflicts and ensure your project uses the correct Python packages.
- Use the VS Code Testing panel: Running and debugging tests visually gives faster feedback and simplifies test execution.
Following these best practices ensures a smooth experience while learning Playwright Python.
Conclusion
Playwright with Python offers a powerful and easy-to-use automation framework for end-to-end web testing.
- It supports cross-browser testing, modern web applications, and advanced features such as screenshots and video recording.
- Beginners can quickly set up tests in VS Code, organize them effectively, and start automating workflows.
Next steps to explore:
- Master selectors and locators to interact with page elements more effectively.
- Learn about fixtures for test setup and teardown.
- Integrate tests into CI/CD pipelines(GitHub Actions) for automated builds and deployment testing.
Start experimenting with your first test, capture a screenshot or video, and gradually build more complex automation scripts. Playwright Python makes learning automation both fun and practical!
Frequently Asked Questions (FAQs)
1. What is Playwright Python?
Playwright Python is a Python library for end-to-end browser automation. It supports Chromium, Firefox, and WebKit, making cross-browser testing easy.
2. Do I need prior Python experience to use Playwright?
Basic Python knowledge is helpful, but not mandatory. Beginners can start with simple tests and gradually explore advanced features.
3. How do I install Playwright and its supported browsers?
Install Playwright via pip: pip install playwright pytest-playwright
and then run playwright install
to download supported browsers.
4. Where are screenshots and videos saved?
Screenshots are saved in the path you specify in page.screenshot(path="...")
. Videos are saved in the folder defined by record_video_dir
when creating a browser context.
5. Should I use sync or async Playwright?
For beginners and pytest integration, use the **sync API**. Async API is only needed for advanced parallel execution and requires extra plugins.

Hi, I’m Aravind — a seasoned Automation Test Engineer with over 17 years of hands-on experience in the software testing industry. I specialize in tech troubleshooting and tools like Selenium, Playwright, Appium, JMeter, and Excel automation. Through this blog, I share practical tutorials, expert tips, and real-world insights to help testers and developers improve their automation skills.In addition to software testing, I also explore tech trends and user-focused topics, including Snapchat guides, codeless test automation, and more.