This guide walks you through setting up Playwright with Python in Visual Studio Code, from installing the necessary tools to writing and running your first browser automation script. By the end, you’ll have a working environment for testing web applications or scraping data with Playwright’s reliable, cross-browser automation.
What You’ll Need
- Python 3.8 or newer installed on your system. You can check with
python --versionin your terminal. - Visual Studio Code installed. Download it from the official website if you haven’t already.
- Basic familiarity with the VS Code interface – opening folders, using the integrated terminal, and installing extensions.
- An internet connection – you’ll download Playwright and browser binaries during setup.
Step-by-Step
- Install the Python extension in VS Code. Open VS Code, go to the Extensions view (Ctrl+Shift+X), search for “Python� by Microsoft, and click Install. This gives you IntelliSense, debugging, and a Python interpreter selector.
- Create a project folder. In VS Code, open a new folder (File → Open Folder) where you’ll keep your Playwright scripts. Name it something like
playwright-demo. - Open the integrated terminal. Use the menu Terminal → New Terminal, or press Ctrl+` (backtick). You’ll run all commands here.
- Create a virtual environment (recommended). This keeps your project dependencies isolated. Run:
python -m venv venv
Then activate it:
On Windows:venv\Scripts\activate
On macOS/Linux:source venv/bin/activate
Your terminal prompt should now show(venv). - Install Playwright. With the virtual environment active, run:
pip install playwright
This installs the Python package. - Install the browser binaries. Playwright needs actual browser engines to control. Run:
playwright install
This downloads Chromium, Firefox, and WebKit. It may take a few minutes. If you only need Chromium, you can useplaywright install chromiumto save time. - Select the Python interpreter in VS Code. Press Ctrl+Shift+P, type “Python: Select Interpreter�, and choose the one that points to your virtual environment (it will have
venvin the path). This ensures VS Code uses the right environment. - Write your first script. Create a new file called
first_test.pyin the project folder. Add the following code:from playwright.sync_api import sync_playwright with sync_playwright() as p: browser = p.chromium.launch(headless=False) page = browser.new_page() page.goto("https://example.com") print(page.title()) browser.close()This launches a visible Chromium window, navigates to example.com, prints the page title, and closes the browser.
- Run the script. In the terminal, run:
python first_test.py
You should see a browser window open, then the title “Example Domain� printed in the terminal. - Optionally, install pytest for test structure. If you plan to write formal tests, install pytest and the Playwright pytest plugin:
pip install pytest pytest-playwright
Then you can use the@pytest.fixturedecorators to manage browser lifecycles.
Common Mistakes
- Forgetting to activate the virtual environment. If you see
ModuleNotFoundError: No module named 'playwright', it’s almost always because the venv isn’t active or you’re using a different Python interpreter. - Running
playwright installoutside the venv. The command installs browsers globally, but it’s cleaner to run it inside the project venv so the version matches the package. - Using the wrong Python interpreter in VS Code. Even if the terminal works, VS Code’s debugger might use a different interpreter. Always select the venv interpreter as shown in Step 7.
- Not closing the browser. If you leave
browser.close()out, your script may hang or leave zombie processes. Always close the browser or use a context manager. - Mixing sync and async APIs. Playwright offers both
sync_playwrightandasync_playwright. Don’t mix them in the same script – pick one style and stick with it.
Tips
- Use
headless=Falsefor debugging so you can see what the browser is doing. When you’re confident the script works, setheadless=Truefor faster execution. - Take screenshots easily. Add
page.screenshot(path="screenshot.png")to capture the page state – useful for debugging or documentation. - Use the Playwright Codegen tool. Run
playwright codegen https://example.comin the terminal. It opens a browser where you can click around, and it generates the Python code for your actions – a great way to learn selectors. - Organize selectors in a separate file. For larger projects, keep CSS/XPath selectors in a constants file so they’re easy to update when the UI changes.
- Use the VS Code debugger. Set breakpoints in your script and press F5 to run with debugging. This lets you inspect variables and page state step by step.
- Keep your dependencies documented. Run
pip freeze > requirements.txtso you can recreate the environment later.
Troubleshooting
- “Executable doesn’t existâ€� error. This means the browser binaries aren’t installed. Run
playwright installagain and ensure you’re in the correct environment. - “ModuleNotFoundError: No module named ‘playwright’â€� – Check that the venv is active (you see
(venv)in the terminal) and that you installed Playwright withpip install playwright. - Browser opens but script hangs. This often happens if you forget
browser.close()or if there’s an open dialog. Make sure your script reaches the close call, or use atry/finallyblock. - Permission errors when installing browsers on Linux. You may need to install system dependencies. Run
playwright install-deps(requires sudo) to install them automatically. - VS Code debugger doesn’t find Playwright. Re-select the Python interpreter (Step 7) and try again. Also check that the venv is active in the debugger’s terminal.
- “TimeoutError� when navigating. Increase the timeout with
page.goto(url, timeout=60000)or check your internet connection. Some sites are slow to respond.
Frequently Asked Questions
Do I need to install browsers separately?
Yes, Playwright does not bundle browsers with the pip package. You must run playwright install to download them. This is a one-time step per project.
Can I use Playwright with an existing browser like Chrome?
Yes, you can use p.chromium.launch(channel="chrome") to use your installed Chrome instead of the bundled Chromium. This is useful for testing in a specific browser version.
What’s the difference between sync and async API?
The sync API is simpler for beginners – it runs each command sequentially. The async API uses async/await and is better for concurrent tasks. For basic automation, start with sync.
How do I run tests with pytest?
Install pytest-playwright, then write test functions that accept a page fixture. Run pytest in the terminal. The plugin handles browser launch and teardown automatically.
Can I scrape data with Playwright?
Yes, Playwright is excellent for scraping dynamic sites. Use page.locator() to extract text, attributes, or HTML. Remember to respect the site’s terms of service and robots.txt.
Why does my script work in terminal but not in VS Code debugger?
This is usually an interpreter mismatch. Press Ctrl+Shift+P, choose “Python: Select Interpreter�, and pick the one with venv in the path. Then restart the debugger.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Blockchain vs Database: When Does Distributed Trust Win?
- Satellite Internet Equipment & Installation Guide
- GTA 6 and Call of Duty on Same Console?
- MCP in Production: Security Failures Behind the Backdoor Label
- Nothing Phone 2 Waterproof? IP54 Rating Explained
- Infinix GT 20 Pro vs AI Nova 2 Ultra 5G: Which Fits?