MCP Playwright Screenshots: Guide to Browser Captures
In this article
Taking browser screenshots with the Model Context Protocol (MCP) using the Playwright server allows you to programmatically capture visual states of websites, debug UI layouts, and verify page rendering. By connecting an MCP-compatible client to the Playwright server, you can send commands to navigate to a URL, interact with elements, and save a PNG or JPEG image of the viewport or a specific element to your local file system.
What You'll Need
- An MCP-compatible host application (such as Claude Desktop or other IDEs supporting MCP).
- The Playwright MCP server installed and configured in your
config.json. - Node.js and npm installed on your machine to run the server.
- Playwright browser binaries (Chromium, Firefox, or WebKit) installed via
npx playwright install. - A valid target URL that is publicly accessible or reachable from your local network.
Step-by-Step
- Configure the MCP Server: Open your MCP configuration file (e.g.,
claude_desktop_config.json). Add the Playwright server definition. Ensure the command points to the correct node path and the server script, typically usingnpx -y @modelcontextprotocol/server-playwright. - Initialize the Connection: Restart your MCP host application. Verify that the Playwright tools (such as
playwright_navigateandplaywright_screenshot) are listed as available tools in the interface. - Navigate to the Target Page: Use the
playwright_navigatetool. Provide the full URL of the website you wish to capture. Wait for the tool to confirm that the page has loaded successfully. - Prepare the Viewport: If the page requires a specific resolution or a mobile view, use the
playwright_set_viewporttool (if available in your server version) to define the width and height. This ensures the screenshot captures the intended layout. - Execute the Screenshot Command: Call the
playwright_screenshottool. You will typically need to provide a filename or a path where the image should be saved. If you only want a specific part of the page, you can pass a CSS selector to target a specific element. - Verify the Output: Check the designated output directory on your local machine. Open the resulting image file to ensure the capture is clear and contains the expected content.
- Clean Up the Session: Once the capture is complete, close the browser instance using the appropriate shutdown command or by restarting the MCP server to free up system memory.
Common Mistakes
- Missing Browser Binaries: Attempting to take a screenshot before running
npx playwright install. This results in a "browser not found" error. - Incorrect Selectors: Providing a CSS selector for an element that doesn't exist or is hidden, leading to empty images or tool errors.
- Ignoring Load Times: Taking a screenshot immediately after navigation before dynamic content (like JavaScript frameworks) has finished rendering the page.
- Permission Issues: Saving screenshots to a directory where the MCP host does not have write permissions.
Tips
- Full Page Captures: If the server supports it, enable the
fullPage: trueoption to capture the entire scrolling length of the page rather than just the visible viewport. - Wait for Selectors: Before triggering the screenshot, use a "wait for selector" command to ensure a specific element is visible, which guarantees the page is fully loaded.
- Headless Mode: Keep the browser in headless mode for faster performance, but switch to headed mode during debugging to see where the cursor is clicking.
- Naming Conventions: Use timestamps in your filenames (e.g.,
screenshot_20261202.png) to avoid overwriting previous captures during iterative testing.
Troubleshooting
If the screenshot tool fails to return an image, first check the MCP server logs. If you see a timeout error, the website may be blocking automated scrapers; try adjusting the user-agent string or checking if the site requires authentication.
If the image is blank or shows a loading screen, the page likely uses asynchronous loading. Insert a short delay or a "wait for load state" command before calling the screenshot tool. If the tool reports that it cannot find the browser, re-run the npx playwright install command in your terminal to ensure all drivers are up to date.
For issues where the screenshot is cut off, verify the viewport dimensions. Some websites use sticky headers or modals that can interfere with full-page captures; try capturing specific elements instead of the whole page.
Frequently Asked Questions
Can I take screenshots of pages behind a login?
Yes, but you must first use the Playwright tools to navigate to the login page, fill in the credentials using playwright_fill, and click the submit button before triggering the screenshot.
Which image format is used for the screenshots?
By default, Playwright captures images in PNG format, though some configurations allow for JPEG. PNG is preferred for UI captures due to its lossless quality.
Does this work on mobile websites?
Yes. By setting the viewport size to common mobile dimensions (e.g., 375x812) via the MCP tools, Playwright will render the mobile version of the site.
Can I capture a specific element instead of the whole page?
Yes, by providing a CSS selector (like .main-content or #header) to the screenshot tool, Playwright will crop the image to only that specific element.
Is there a limit to how many screenshots I can take?
There is no hard limit within the protocol, but frequent screenshots can consume significant disk space and memory. It is best to close browser contexts between large batches.