Playwright MCP: How to Use It for Browser Automation
In this article
Playwright MCP (Model Context Protocol) lets you control a real browser through an AI assistant or any MCP-compatible client. Instead of writing and debugging Playwright scripts by hand, you give natural-language instructions and the MCP server translates them into browser actions like clicking, typing, navigating, and extracting data. This guide walks you through setting it up and using it effectively.
What You'll Need
- Node.js 18+ (or Python 3.9+ if you prefer the Python version) installed on your machine.
- An MCP client – examples include Claude Desktop, VS Code with the MCP extension, or any custom app that supports MCP.
- Playwright MCP package – install it via npm or pip (see steps below).
- Basic familiarity with your terminal – you'll run commands to install and configure.
Step-by-Step
- Install Playwright MCP
In your terminal, run one of these depending on your setup:
npm install -g @playwright/mcp(global) ornpm install @playwright/mcp(local project). For Python:pip install playwright-mcp. After installation, runplaywright installto download browser binaries if you haven't already. - Configure your MCP client Open your MCP client's configuration file (e.g., claude_desktop_config.json for Claude Desktop, or the MCP settings in VS Code). Add a new server entry pointing to the Playwright MCP executable. For example: For Python, use playwright-mcp as the command. Save the file and restart your client.
- Verify the connection
In your MCP client, look for a list of available tools (often under a tools or MCP panel). You should see tools like
browser_navigate,browser_click,browser_type,browser_snapshot, etc. If you don't see them, check the client's logs for errors. - Start a simple automation Give a natural-language instruction like: "Open example.com and tell me the page title." The MCP server will launch a browser (usually headed, so you can watch) and execute the steps. You can chain multiple actions in one request: "Go to google.com, type 'playwright mcp' in the search box, press Enter, and list the first three result titles."
- Use the snapshot tool for complex pages If the AI gets confused about which element to click, ask it to "take a snapshot" or "get the page snapshot." This returns a simplified accessibility tree of the page, showing interactive elements and their labels. You can then reference specific elements by their snapshot ID or text.
- Handle navigation and waits For pages that load slowly, instruct the AI to "wait for the element with text 'Login' to appear" or "wait for 2 seconds." The MCP tools include wait mechanisms, but explicit instructions help avoid flaky behavior.
- Extract data and take screenshots Ask for "the text of the main heading" or "take a screenshot and save it as screenshot.png." The MCP server can return data as structured text and save screenshots to your local file system.
- Close the browser when done Say "close the browser" or "stop the session." This frees resources and prevents orphan processes. If you forget, the MCP server usually terminates after a timeout, but it's good practice.
Common Mistakes
- Not installing browser binaries – Running
playwright installis essential; otherwise, the browser fails to launch with a "executable doesn't exist" error. - Using the wrong command in config – For global installs, use the full path or
npx; for Python, ensure the package is in your PATH. Double-check the command by running it manually in your terminal. - Asking for actions on pages that require login – The MCP browser is a fresh profile; it won't have your existing cookies. You'll need to log in manually or provide credentials (but avoid storing sensitive info in the config).
- Ignoring pop-ups and dialogs – If a page has a modal, the AI might get stuck. Ask it to "dismiss any dialogs" or "click the close button on the popup."
- Overloading a single request – Trying to do 10 steps in one instruction often fails. Break it into smaller tasks and verify each step.
Tips
- Use headed mode for debugging – By default, the browser is visible. If you want headless (invisible), you can pass
--headlesswhen starting the MCP server, but for learning, keep it headed. - Leverage the snapshot tool – It's your best friend when the AI misidentifies an element. Ask for a snapshot and then reference the exact text or role.
- Be explicit about waits – Instead of "click the button," say "wait for the button with text 'Submit' to be enabled, then click it."
- Test selectors manually – If the AI picks the wrong element, you can ask it to "use the CSS selector #login-form input[type='email']" – you can find selectors using your browser's dev tools.
- Keep your MCP client updated – Both the client and the Playwright MCP package are under active development; updates often fix bugs and add features.
Troubleshooting
- Browser doesn't launch – Run
playwright installto download browsers. If you're on Linux, you may need system dependencies; check Playwright's official docs for the install command (npx playwright install-deps). - Tools not showing up in client – Restart your MCP client after adding the server. Check the client's logs for errors. Also verify that the command in your config runs without errors when executed manually.
- Actions fail with "element not found" – The page might not have loaded fully. Ask the AI to wait or take a snapshot to see what's actually on the page. Sometimes the element is inside an iframe – mention that in your instruction.
- MCP server crashes on complex pages – Try simplifying the task, or use the snapshot to get a cleaner view. If it's a known bug, update the package to the latest version.
- Can't interact with file uploads – Playwright MCP supports file uploads via the
browser_upload_filetool. Provide the absolute path to the file. If it doesn't work, try using a relative path from the current working directory.
Frequently Asked Questions
Can I use Playwright MCP with any MCP client?
Yes, as long as the client supports the MCP protocol (e.g., Claude Desktop, VS Code, Cursor, or custom apps). The setup is similar across clients – you define a server entry in the client's config.
Does Playwright MCP work with all browsers?
It supports Chromium, Firefox, and WebKit by default. You can specify a browser via the --browser option when starting the server (e.g., --browser firefox).
Can I use my existing browser profile (with cookies and logins)?
Not directly. The MCP server launches a fresh browser instance. You can manually log in during the session, but the profile is temporary and won't persist between sessions unless you configure a persistent profile directory (advanced).
Is it safe to use Playwright MCP on sensitive websites?
It's as safe as any automation tool – it performs actions you instruct. However, avoid entering sensitive credentials into the MCP client's chat, as they may be logged. Use environment variables or manual login steps instead.
How do I take a screenshot with Playwright MCP?
Simply ask the AI to "take a screenshot" or "capture the page." The MCP server will save a PNG file to your current working directory (or a specified path).
Can I run Playwright MCP in headless mode?
Yes, start the server with --headless to run without a visible browser window. This is useful for automated CI pipelines or when you don't need to watch the actions.