User Agents

Web browsers and everyday web tech, explained

Playwright MCP for Web Search & Browser Automation

· 5 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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

  1. 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.
  2. Install Playwright browsers: Run npx playwright install chromium to download the Chromium browser engine. You can also install Firefox or WebKit if needed, but Chromium is the default and most stable.
  3. 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.
  4. 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, and browser_snapshot.
  5. Perform a web search: Ask your AI assistant to "search for [query]" or use the browser_navigate tool directly with a search engine URL like https://www.google.com/search?q=your+query. The browser will open and load the results.
  6. Extract information: Use the browser_snapshot tool 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.
  7. Interact with elements: To click a link or button, use browser_click with the element's text or accessibility ID from the snapshot. For example, browser_click(text="Next").
  8. Fill forms: Use browser_type to enter text into input fields. First, find the field's selector from the snapshot, then call browser_type(selector="#search", text="playwright").
  9. Take screenshots: Use browser_screenshot to capture the current viewport or full page. Save the screenshot to a file by specifying a path in the tool's options.
  10. Handle navigation and waits: For pages that load dynamically, use browser_wait to pause until a selector appears or a timeout occurs. This prevents errors from trying to interact with elements before they exist.
  11. End the session: When done, close the browser using browser_close or 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 install will 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_wait after navigation if the page is dynamic.
  • Overloading the AI with long pages: Snapshotting a very long page can exceed context limits. Use browser_snapshot with a specific selector to limit the view.
  • Ignoring pop-ups and dialogs: Unexpected alert boxes can block automation. Use browser_handle_dialog to accept or dismiss them.

Tips

  • Use headless mode for speed: Set the --headless flag 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 like libnss3).
  • Timeout errors on navigation: Increase the default timeout using the --timeout flag when starting the server, or use browser_wait with 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.