Why Playwright Won't Open a Browser & How to Fix It
In this article
- What Is Playwright and Why Does It Need a Browser?
- Common Reasons Playwright Fails to Launch a Browser
- Step-by-Step Fixes for Browser Launch Failures
- How to Choose the Right Browser and Launch Options
- Common Problems and Their Specific Solutions
- Comparing Playwright with Selenium and Puppeteer
- Safety and Best Practices When Using Playwright
- Frequently Asked Questions
Playwright won't open a browser because the browser binaries are not installed, the executable path is incorrect, or system dependencies are missing. To fix it, run npx playwright install to download the required browsers, then ensure your code specifies the correct browser executable path.
What Is Playwright and Why Does It Need a Browser?
Playwright is an open-source automation library for web testing and scraping. It controls real Chromium, Firefox, and WebKit browsers via a single API. Unlike tools that simulate a browser environment, Playwright launches an actual browser process, which means the browser must be present on the system where the script runs. If the browser isn't found, Playwright throws an error like "Executable doesn't exist" or "Browser not found."
Common Reasons Playwright Fails to Launch a Browser
- Missing browser binaries: The most frequent cause. Playwright does not bundle browsers with the npm package by default; you must run
npx playwright install. - Wrong executable path: You may have specified a custom path that points to a non-existent file.
- Missing system dependencies: On Linux, browsers require libraries like libnss3, libatk, etc. Without them, the browser crashes on launch.
- Permissions issues: The browser may not have execute permission, or the user lacks rights to run it.
- Environment variables: Variables like
PLAYWRIGHT_BROWSERS_PATHmay point to a location where browsers aren't installed. - Firewall or proxy interference: Rare, but some corporate networks block browser processes.
Step-by-Step Fixes for Browser Launch Failures
- Install the browsers: Run
npx playwright installin your project directory. This downloads the default browser versions (Chromium, Firefox, WebKit) to a cache folder. - Verify installation: Check the output for errors. If you see a message like "Host system is missing dependencies," run
npx playwright install-deps(Linux only) to install required system libraries. - Check your code: Ensure you're not passing a
executablePaththat overrides the default. If you are, verify that path exists and is executable. - Set the correct environment: If you're using a CI environment, set
PLAYWRIGHT_BROWSERS_PATH=0to install browsers locally in the project, or set it to a custom path and install there. - Clear cache: Sometimes the browser cache is corrupted. Delete the
~/.cache/ms-playwrightfolder (on Linux/macOS) or%USERPROFILE%\AppData\Local\ms-playwright(on Windows) and reinstall. - Test with a simple script: Create a minimal script that launches Chromium and closes it. If that works, the issue is in your code.
How to Choose the Right Browser and Launch Options
Playwright supports three browser engines: Chromium, Firefox, and WebKit. Each has different rendering behavior. For most tests, Chromium is the default and works well. To choose a browser, use chromium.launch(), firefox.launch(), or webkit.launch(). You can also pass options like headless: false to see the browser window, or slowMo to slow down actions for debugging. If you need a specific browser version, specify it in the playwright.config.js file using the use object.
Common Problems and Their Specific Solutions
- Error: "Executable doesn't exist" – Run
npx playwright installand ensure the path is correct. - Error: "Host system is missing dependencies" – Run
npx playwright install-deps(Linux only). - Error: "Protocol error" – This often happens when the browser crashes. Check system resources and try launching with
--no-sandboxif running as root in a container. - Browser opens but closes immediately – Check for unhandled exceptions in your script, or try
headless: falseto see what happens. - Works locally but not in CI – Ensure your CI image has the necessary dependencies and that you run
npx playwright installas part of the build.
Comparing Playwright with Selenium and Puppeteer
Playwright is often compared to Selenium and Puppeteer. Unlike Selenium, Playwright does not require a separate WebDriver server; it communicates directly with the browser. Puppeteer is similar but only supports Chromium, while Playwright supports three engines. Playwright also has built-in auto-waiting and web-first assertions, making it more reliable for modern web apps. If you're switching from Puppeteer, note that Playwright's API is similar but not identical; you'll need to adjust launch options and selectors.
Safety and Best Practices When Using Playwright
When using Playwright for web scraping or testing, always respect the website's terms of service and robots.txt. Avoid overloading servers with too many requests; use delays if necessary. For security, never hardcode credentials in your scripts; use environment variables. Also, be aware that running browsers in headless mode can sometimes be detected by websites; if you need to avoid detection, use stealth techniques, but be aware this may violate terms of service. Always keep your Playwright version updated to get bug fixes and security patches.
Frequently Asked Questions
Why does Playwright say "browserType.launch: Executable doesn't exist"?
This error means Playwright cannot find the browser binary. Run npx playwright install to download the browsers. If you've already installed, check the PLAYWRIGHT_BROWSERS_PATH environment variable; it might point to a different location.
How do I install Playwright browsers on Linux?
Run npx playwright install and then npx playwright install-deps to install system dependencies. The latter requires sudo privileges. If you're in a Docker container, ensure the base image has the necessary libraries.
Can I use an existing Chrome or Firefox installation with Playwright?
Yes, you can use the executablePath option in the launch method to point to your system's browser executable. However, this is not recommended because Playwright is tested with its own bundled versions, and using a different version may cause compatibility issues.
What does "browser closed unexpectedly" mean?
This error occurs when the browser process crashes during a test. It can be due to missing system dependencies, insufficient memory, or a bug in the browser. Check the system logs and try increasing memory or using a different browser engine.
How do I fix Playwright not working in Docker?
Use the official Playwright Docker image (mcr.microsoft.com/playwright) which has all dependencies pre-installed. If you use a custom image, ensure you run npx playwright install-deps and install the browsers.
Is it possible to run Playwright without downloading browsers?
No, Playwright requires a browser to automate. However, you can use the channel option to use Chrome or Edge installed on your system, but you still need to install the browser binaries for Firefox and WebKit if you want to use them.