← Blog

BLOG

Claude Code Web Browser: Connect to Remote Chromium

Connect a Claude Code web browser workflow to remote Chromium with Playwright over CDP, then inspect pages and run repeatable browser tasks.

9 min readRemote Browser
Ivory computer towers connected across terracotta platforms in a quiet desert landscape
The infrastructure behind your browserREMOTE BROWSER / ILLUSTRATION

A Claude Code web browser workflow gives Claude Code a way to inspect and interact with real web pages while it works in your project. A practical setup is to run a Playwright script from the terminal and connect that script to a hosted Chromium session over the Chrome DevTools Protocol (CDP).

This keeps browser execution outside the coding agent’s process while letting Claude Code edit the script, run it, and use its output. You can use the same pattern for a remote page check, an authenticated workflow, or a repeatable UI task. The key decisions are how Claude Code invokes browser actions, where browser state lives, and what the script is allowed to change.

How the connection works

Claude Code does not get browser access simply because a remote browser endpoint exists. Your project needs an integration layer that can connect to that endpoint and expose useful results or actions.

With a Playwright script, the flow is:

  1. Create or select a hosted Chromium session in Remote Browser.
  2. Pass its CDP endpoint to a local Node.js process through an environment variable.
  3. Have Playwright connect to the session and perform a defined task.
  4. Return a small result to the terminal, where Claude Code can read it.

Remote Browser provides hosted Chromium sessions and CDP access, and supports Playwright. This means the browser runs remotely while your script and Claude Code run in your development environment. See the Remote Browser documentation for connection details and session setup.

A script is a good starting point when the task has clear inputs and outputs. If you need Claude Code to choose browser actions interactively, you can build on the same connection with a tool server that exposes a limited set of browser operations. That tool server is a separate integration layer; confirm that it supports your chosen CDP session and access controls.

Choose an integration that fits the task

There are three common ways to give a coding agent browser access. The right choice depends on whether you need repeatable automation, interactive control, or a fast local feedback loop.

ApproachBest fitTrade-off
Local PlaywrightTesting a local page or debugging during developmentRuns on the developer’s machine and uses its browser installation, network, and profile
Playwright script connected over CDPRunning a known task in hosted ChromiumRequires a session endpoint and deliberate handling of session state and cleanup
Browser tool server connected over CDPLetting the agent select from browser actions as it worksAdds another service and tool interface to configure and constrain

For a first implementation, use a script that accepts a target URL and returns a concise result. This is easier to review than giving an agent unrestricted access to browser actions. Once the task is stable, you can decide whether it needs interactive control.

For background on the runtime choices, see remote browsers for AI agents. If the main requirement is controlling a browser from another process, remote browser control covers that pattern.

Connect Playwright to a hosted session

Install Playwright and a TypeScript runner in the project:

npm install playwright
npm install --save-dev tsx

Create a session in Remote Browser and obtain its CDP endpoint using the provider’s documented session flow. Supply that endpoint as REMOTE_BROWSER_CDP_URL. Treat the endpoint as a secret: avoid committing it to source control or printing it in logs.

Save this script as scripts/inspect-page.ts:

import { chromium } from "playwright";

const cdpUrl = process.env.REMOTE_BROWSER_CDP_URL;
const targetUrl = process.argv[2];

if (!cdpUrl) {
  throw new Error("Set REMOTE_BROWSER_CDP_URL before running this script.");
}

if (!targetUrl) {
  throw new Error("Pass a page URL as the first argument.");
}

const browser = await chromium.connectOverCDP(cdpUrl);

try {
  const context = browser.contexts()[0] ?? await browser.newContext();
  const page = context.pages()[0] ?? await context.newPage();

  await page.goto(targetUrl, {
    waitUntil: "domcontentloaded",
    timeout: 30_000,
  });

  const heading = await page.locator("h1").first().textContent().catch(() => null);

  console.log(JSON.stringify({
    url: page.url(),
    title: await page.title(),
    heading: heading?.trim() ?? null,
  }));
} finally {
  // Closing the browser may end the hosted session. Follow your
  // provider's lifecycle rules if the session needs to remain available.
  await browser.close();
}

Run it with the endpoint set in the shell environment:

REMOTE_BROWSER_CDP_URL="your-session-endpoint" \
  npx tsx scripts/inspect-page.ts "https://example.com"

The script connects through Playwright’s connectOverCDP method, which attaches to a Chromium browser using a CDP endpoint. Read the Playwright `connectOverCDP` reference for supported behavior and limitations.

The example reuses the first available browser context and page when the hosted session provides them. If there is no page, it creates one. For a task that needs a clean context, change the script to create a new one and check how your provider handles that context when the session ends. A persistent profile and a running browser session are related but distinct: preserving cookies or local storage depends on the profile and session configuration you select.

Let Claude Code run a bounded browser task

Claude Code can invoke the script from the project terminal, inspect its output, and make changes to the script as needed. A useful first task is to ask it to run the inspection against a page and report the title, heading, and final URL. That creates a short feedback loop without asking the agent to infer what happened inside the browser.

For repeatable work, make the script’s inputs explicit. For example, accept the target URL as a command-line argument and return structured JSON with a known set of fields. This makes results easier for Claude Code to parse and compare. Avoid returning entire page contents by default; large outputs are hard to review and may include private data.

If you want Claude Code to choose actions rather than run a fixed script, define a small interface around the browser. Useful operations might include:

  • Open an approved URL and report the page title.
  • Read text from a named locator.
  • Click a specific button and report whether a confirmation appeared.
  • Capture a screenshot for a local review step.
  • Return the current URL and a concise error when navigation fails.

Keep the interface task-focused. A tool that accepts arbitrary JavaScript or unrestricted navigation is difficult to review and can make unintended changes. For a form submission or account action, require a human review point before the script submits data.

Handle sessions and profiles deliberately

Hosted sessions introduce lifecycle choices that do not arise in exactly the same way with a local browser. Decide how each run gets a session, which profile it uses, and when the session should end. Remote Browser supports persistent profiles and session isolation; use them according to the data and parallelism needs of your workflow.

A few practical rules help keep runs understandable:

  • Use separate sessions for independent tasks that should not share cookies or page state.
  • Select a persistent profile only when the workflow needs browser state across sessions.
  • Close sessions when the task is complete, following the provider’s lifecycle behavior.
  • Keep the CDP endpoint in an environment variable or secret store, not in source code or terminal output.
  • Send only the browser result needed for the next step back to Claude Code.

Do not assume that a profile setting makes a session safe to share among unrelated tasks. A shared profile can carry authentication state, preferences, and other site data from one run to the next. If a workflow involves customer accounts or internal systems, scope profile access and session reuse accordingly.

Production criteria and trade-offs

A remote browser can make execution independent of a developer laptop and provide a consistent place to run browser work. It also adds a network connection, a session lifecycle, and provider configuration that local Playwright does not need. Those trade-offs matter when a task moves from a one-off script into a scheduled job or team workflow.

Review the following before relying on a Claude Code web browser workflow in production:

Reliability

Use explicit navigation timeouts and wait for a meaningful page condition before reading data. domcontentloaded only indicates that the initial HTML document has been parsed; client-side content may still be loading. For dynamic pages, wait for a locator that signals the page is ready:

await page.goto(targetUrl, { waitUntil: "domcontentloaded" });
await page.getByRole("heading", { name: "Account overview" }).waitFor();

Keep retries limited to failures that are safe to retry. A navigation timeout may be transient; a click that submits a payment or changes an account may not be safe to repeat without checking the resulting state first.

Observability

Return enough detail to diagnose failures without dumping secrets or full page contents. Include the target and final URL, a task identifier, and a short error message. If your team needs screenshots or live inspection, use the session viewing options supported by your Remote Browser setup. Live visibility helps with debugging, but it does not replace clear script output or error handling.

Permissions

The browser acts with the permissions of its current session. A persisted login can let an automation read or change account data. Restrict which pages and actions a script can reach, and avoid giving a generic agent workflow access to high-impact controls unless the task requires them. For state-changing actions, add explicit checks and a review step.

Cost and capacity

Hosted browser usage depends on the provider’s current pricing and usage limits. Check Remote Browser pricing before scheduling frequent runs or increasing parallel work. Estimate workload from expected session duration and concurrency, and account for time spent debugging or waiting on slow pages. Do not assume a browser session is free to keep open when idle.

Browser configuration

Use the configurable browser settings your task requires, such as an appropriate proxy or browser configuration where available. Test the actual target sites with the chosen settings. A remote Chromium session does not guarantee that every site permits automation or that every interaction will succeed.

A practical rollout

Start with one read-only workflow and a single hosted session. Confirm that the endpoint is loaded securely, that Playwright can connect, and that the script returns the expected output. Then add the exact actions the task needs and test how the session behaves when the script exits.

Before sharing the workflow with a team, document how to provide the endpoint, select a profile, and end the session. Add clear handling for navigation errors and any state-changing step. Keep browser output small and structured so Claude Code can use it without copying sensitive page data into the conversation.

This approach gives Claude Code access to a real browser through familiar Playwright code, while leaving the runtime and session lifecycle explicit. For a fixed browser task, a script is often enough. For agent-selected actions, add a constrained tool interface and review its permissions before using it on authenticated or production systems.