Playwright MCP for Web Search & Browser Automation
In this article
Playwright MCP (Model Context Protocol) lets you control a real browser through an AI assistant or script, enabling automated web searches, form filling, screenshot capture, and multi-step interactions. This guide walks you through setting up and using Playwright MCP for reliable browser automation.
What You'll Need
- Node.js 18+ installed on your system
- A code editor or terminal
- Basic familiarity with command-line tools
- An MCP-compatible client (e.g., Claude Desktop, VS Code with MCP extension, or a custom script using the MCP SDK)
Step-by-Step
- Install Playwright MCP globally: Open your terminal and run
npm install -g @playwright/mcp. This installs the MCP server package that bridges your AI client to the browser. - Install Playwright browsers: Run
npx playwright install chromiumto download the Chromium browser engine. You can also install Firefox or WebKit if needed, but Chromium is the default and most stable. - Configure your MCP client: In your client (e.g., Claude Desktop), add a new MCP server with the command
npx @playwright/mcp@latest. Provide the full path if the global install isn't recognized. For VS Code, use the MCP extension and add the same command in its settings. - Start a session: Launch your MCP client and initiate a new conversation. The Playwright MCP tools will be available automatically. Common tools include
browser_navigate,browser_click,browser_type, andbrowser_snapshot. - Perform a web search: Ask your AI assistant to "search for [query]" or use the
browser_navigatetool directly with a search engine URL likehttps://www.google.com/search?q=your+query. The browser will open and load the results. - Extract information: Use the
browser_snapshottool to get a text representation of the current page. This snapshot includes accessibility tree data, which the AI can read to understand links, buttons, and text content. - Interact with elements: To click a link or button, use
browser_clickwith the element's text or accessibility ID from the snapshot. For example,browser_click(text="Next"). - Fill forms: Use
browser_typeto enter text into input fields. First, find the field's selector from the snapshot, then callbrowser_type(selector="#search", text="playwright"). - Take screenshots: Use
browser_screenshotto capture the current viewport or full page. Save the screenshot to a file by specifying a path in the tool's options. - Handle navigation and waits: For pages that load dynamically, use
browser_waitto pause until a selector appears or a timeout occurs. This prevents errors from trying to interact with elements before they exist. - End the session: When done, close the browser using
browser_closeor just end the conversation. The browser process will terminate automatically after a timeout.
Common Mistakes
- Forgetting to install browsers: Running the MCP server without
npx playwright installwill cause navigation errors because no browser engine exists. - Using wrong selectors: CSS selectors from DevTools may not match the accessibility tree. Always use the snapshot's selectors (e.g., ).
- Not waiting for elements: Trying to click or type before a page finishes loading leads to 'element not found' errors. Always use
browser_waitafter navigation if the page is dynamic. - Overloading the AI with long pages: Snapshotting a very long page can exceed context limits. Use
browser_snapshotwith a specific selector to limit the view. - Ignoring pop-ups and dialogs: Unexpected alert boxes can block automation. Use
browser_handle_dialogto accept or dismiss them.
Tips
- Use headless mode for speed: Set the
--headlessflag when starting the MCP server to run without a visible browser window. This is faster for automated tasks. - Leverage the accessibility tree: The snapshot is your best friend. It gives you a clean, text-based view of the page, making it easier for the AI to decide what to do next.
- Combine tools for complex tasks: For a multi-step flow like logging in, navigate to the login page, wait for the form, type credentials, click submit, then wait for the dashboard.
- Use browser_navigate with full URLs: Always include the protocol (https://) to avoid errors.
- Keep sessions short: Long-running sessions may hit memory issues. Close the browser periodically and start fresh if performance degrades.
Troubleshooting
- Browser doesn't launch: Ensure Playwright browsers are installed with
npx playwright install. Check that your system meets the dependencies (e.g., on Linux, you may need to install additional libraries likelibnss3). - Timeout errors on navigation: Increase the default timeout using the
--timeoutflag when starting the server, or usebrowser_waitwith a longer timeout. - Element not found: Re-snapshot the page after waiting. The element may have appeared or the selector may have changed. Use a more specific selector from the new snapshot.
- MCP server not recognized: Verify the command path. If installed globally, use
npx @playwright/mcp@latest; if not, install locally in your project. - Permission errors on Linux: Playwright requires certain system libraries. Follow the official Playwright troubleshooting guide for your OS.
Frequently Asked Questions
Can I use Playwright MCP with any AI assistant?
It works with any MCP-compatible client. Popular options include Claude Desktop, VS Code with the MCP extension, and custom scripts using the MCP SDK.
Is Playwright MCP free?
Yes, it's open-source and free to use. You only need to pay for the browser resources on your machine.
Can it automate sites that require login?
Yes, you can navigate to the login page, fill in credentials, and submit. However, be mindful of terms of service and avoid automating sites that prohibit it.
How do I take a full-page screenshot?
Use the browser_screenshot tool with the fullPage: true option. This captures the entire page, not just the visible viewport.
Does it work with Firefox or WebKit?
Yes, you can install and use those browsers, but Chromium is the default and most reliable. Change the browser by passing the --browser flag when starting the server.
Can I run multiple browser sessions at once?
Yes, but each session requires a separate MCP server instance. You can start multiple servers on different ports and connect them to different clients.