User Agents

Web browsers and everyday web tech, explained

Playwright MCP and CDP Screenshot Capture Guide

· 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

Capturing screenshots using a Playwright Model Context Protocol (MCP) server combined with the Chrome DevTools Protocol (CDP) allows you to programmatically render web pages and save visual snapshots of the DOM. This setup is particularly powerful for automating visual regression testing, generating documentation, or enabling remote agents to "see" the current state of a browser session. By leveraging CDP, you gain low-level access to the browser engine, enabling full-page captures and specific element snapshots that go beyond standard high-level API calls.

What You'll Need

  • Node.js installed on your local machine or server.
  • A Playwright MCP server implementation configured in your environment.
  • A Chromium-based browser (Chrome or Edge) installed.
  • An MCP-compatible client (such as Claude Desktop or a custom MCP host) to send commands to the server.
  • Basic familiarity with JSON-RPC or the specific tool definitions provided by your MCP server.

Step-by-Step

  1. Initialize the MCP Server: Start your Playwright MCP server. Ensure the server has the necessary permissions to launch a browser instance and write files to your local disk. Verify the connection by checking the server logs for a successful startup message.
  2. Launch the Browser Session: Use the MCP tool command to launch the browser. If you are using a headful mode for debugging, ensure the headless flag is set to false. If this is for an automated pipeline, keep it true.
  3. Navigate to the Target URL: Invoke the navigation tool (e.g., navigate or goto) and provide the full URL of the page you wish to capture. Wait for the page to reach a stable state; using networkidle as a wait condition is recommended to ensure all images and stylesheets have loaded.
  4. Establish a CDP Session: To use advanced capture features, the Playwright instance must create a CDP session. In the backend code of the MCP server, this is typically done via page.context().newCDPSession(page). This session allows you to send raw commands directly to the Chrome engine.
  5. Configure Screenshot Parameters: Decide if you need a viewport screenshot or a full-page screenshot. For full-page captures, you must use the CDP command Page.getLayoutMetrics to determine the full height and width of the document, then set the viewport size accordingly using Emulation.setDeviceMetricsOverride.
  6. Execute the Capture Command: Call the screenshot tool provided by the MCP server. If the server supports a capture_screenshot tool, pass the desired filename and path. Under the hood, the server will trigger page.screenshot({ path: 'screenshot.png', fullPage: true }) or the CDP Page.captureScreenshot method.
  7. Verify the Output: Check the designated output directory on your filesystem. Ensure the image is not truncated and that the resolution matches the expected viewport settings.
  8. Close the Session: Always invoke the close tool or terminate the browser instance to free up system memory and prevent orphaned browser processes from running in the background.

Common Mistakes

  • Ignoring Page Load States: Taking a screenshot immediately after the goto command often results in a blank page or a loading spinner. Always wait for domcontentloaded or networkidle.
  • Viewport Mismatch: Forgetting to set the viewport size before capturing a full-page screenshot can lead to images that only capture the "above the fold" content.
  • Permission Errors: Attempting to save screenshots to a directory where the MCP server process does not have write permissions, leading to silent failures or crash logs.
  • Overlooking Dynamic Content: Failing to account for lazy-loading images. Some sites only load images as you scroll; you may need to implement a "scroll-to-bottom" script before capturing.

Tips

  • Use Element-Level Captures: Instead of the whole page, use Playwright's locator API to target a specific CSS selector. This creates cleaner images for UI documentation.
  • Mask Sensitive Data: Use the mask option in the screenshot settings to hide passwords or personal user data with colored boxes before saving the file.
  • Optimize Format: Use JPEG for complex pages with many photos to save disk space, or PNG for UI elements where crisp text and transparency are required.
  • Automate Naming: Use timestamps or unique IDs in your filenames (e.g., screenshot_20261202_1430.png) to avoid overwriting previous captures during iterative testing.

Troubleshooting

If the screenshot returns a blank image, first check if the page requires authentication. If the browser is blocked by a bot-detection system, try adjusting the User-Agent string via the CDP Network.setUserAgentOverride command.

If the MCP server hangs during the capture process, check the browser's memory usage. High-resolution full-page screenshots of extremely long pages can consume significant RAM and may cause the browser to crash. In such cases, try capturing the page in smaller chunks or reducing the viewport scale.

If the file is not appearing in the folder, verify the absolute path provided to the tool. Relative paths in MCP servers can be ambiguous depending on where the host process was launched.

Frequently Asked Questions

Can I capture screenshots in headless mode?

Yes, Playwright is designed to work in headless mode by default, which is faster and uses fewer resources than headful mode.

What is the difference between Playwright screenshots and CDP screenshots?

Playwright screenshots are high-level API calls that are easier to use. CDP screenshots provide raw access to the browser engine, allowing for more granular control over the rendering pipeline.

How do I handle pop-ups or cookie banners in screenshots?

You should use a Playwright locator to find the "Accept" or "Close" button and click it before triggering the screenshot command.

Can I take a screenshot of a page that requires a login?

Yes, you can either automate the login flow first or load a pre-existing browser context that contains the necessary session cookies.

Is it possible to capture a PDF instead of a PNG?

Yes, Playwright provides a page.pdf() method, though this typically requires the browser to be running in headless mode.