← Blog

BLOG

Selenium Or Playwright: Which to Run on Remote Browsers

Selenium or Playwright for hosted Chromium? Compare CDP support, async APIs, auto-waiting, and production fit for AI agents and remote browser sessions.

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

Choosing between Selenium or Playwright is the first real architecture decision in most browser automation projects. The answer depends less on which library is "better" in the abstract and more on what you are driving: a local browser you control end to end, or a hosted Chromium session you connect to over the network. This guide answers the search intent directly, then walks through the trade-offs that actually matter when you run automation against a remote browser runtime.

Short version: for new projects targeting hosted Chromium, Playwright is usually the stronger default because it speaks CDP natively, ships an async API, and handles waiting for you. Selenium remains the right call when you need broad language support, an existing WebDriver grid, or legacy test suites you cannot rewrite. Both work against a remote browser — the connection mechanics differ, and that difference shapes your production code.

The Direct Answer

If you are starting fresh and your target is a remote Chromium session exposed over the Chrome DevTools Protocol (CDP), pick Playwright. It connects to a remote endpoint with one call, exposes browser contexts for session isolation, and its auto-waiting removes a large class of flaky-test bugs before they reach production.

Pick Selenium if your team already runs a Selenium Grid, needs bindings in a language Playwright does not cover well, or maintains a large suite built on the WebDriver protocol. Selenium 4 added CDP access, so it can talk to remote Chromium too — but the ergonomics are rougher and the API surface is smaller.

The rest of this article explains why, with concrete code and production criteria rather than generalities.

How Each Library Connects to a Remote Browser

The connection model is the single biggest practical difference, so start there.

Playwright connects to a remote Chromium instance over CDP using chromium.connectOverCDP(endpointURL). You pass a WebSocket debugging URL, and Playwright returns a Browser object you drive exactly as if it were local. Contexts, pages, and network interception all work across the wire.

Selenium connects through a WebDriver endpoint — either a Selenium Grid URL or a driver that proxies to a remote browser. Selenium 4 also exposes CDP via execute_cdp_cmd (Python) or the devtools package (Java), but you are layering CDP on top of the WebDriver session rather than driving CDP directly.

For a hosted runtime that hands you a CDP WebSocket endpoint, Playwright is the shorter path. Remote Browser exposes hosted Chromium sessions with CDP access, so both libraries can attach — but the Playwright path is a single function call.

Playwright over CDP: a working example

import { chromium, Browser, BrowserContext, Page } from "playwright";

async function runOnRemoteBrowser(cdpEndpoint: string): Promise<void> {
  // Connect to the hosted Chromium session over CDP.
  const browser: Browser = await chromium.connectOverCDP(cdpEndpoint);

  // Reuse the default context or create an isolated one per task.
  const context: BrowserContext =
    browser.contexts()[0] ?? (await browser.newContext());

  const page: Page = await context.newPage();

  try {
    await page.goto("https://example.com", { waitUntil: "domcontentloaded" });

    // Auto-waiting: Playwright resolves the element before acting.
    await page.getByRole("link", { name: "More information" }).click();

    const title = await page.title();
    console.log("Page title:", title);
  } finally {
    await page.close();
    // Do not call browser.close() if the runtime owns the session lifecycle;
    // disconnect instead so the remote session can be reused or reaped.
    await browser.close();
  }
}

Two details matter in production. First, connectOverCDP returns a browser whose contexts may already exist — check browser.contexts() before creating a new one. Second, whether you call browser.close() depends on who owns the session. If the runtime meters browser-hours, closing the connection without tearing down the remote session lets you reuse it; if you want the session gone, close it explicitly. See the documentation for session lifecycle controls.

Feature Comparison for Remote Workloads

CriterionPlaywrightSelenium
Native remote connectionconnectOverCDP(endpoint)WebDriver endpoint or Grid URL
ProtocolCDP-first for ChromiumWebDriver (W3C), CDP layered on top
Async APIFirst-class (async/await)Sync by default; async wrappers vary by binding
Auto-waitingBuilt inManual/explicit waits
Browser contextsNative, cheap isolationNew driver/session per isolation
Language bindingsJS/TS, Python, Java, .NETJS, Python, Java, .NET, Ruby, PHP, and more
Network interceptionNative routing APILimited; CDP-dependent
Best fitNew agent/automation code on hosted ChromiumExisting grids, legacy suites, broad language needs

The table is not a scorecard — it is a map of where each library's assumptions fit. Playwright was designed around a single modern engine family and a tight async model. Selenium was designed around a protocol that abstracts over many browsers and drivers. Those origins still shape the code you write today.

Where Selenium Still Wins

Selenium is not obsolete, and recommending Playwright universally would be lazy. Selenium wins in specific, real situations:

  • Language breadth. If your stack is Ruby, PHP, or another binding Playwright does not cover, Selenium is the pragmatic choice.
  • Existing infrastructure. Teams running a Selenium Grid with mature CI integration should not migrate just because Playwright is trendy. The migration cost is real and the payoff is not automatic.
  • Vendor neutrality. WebDriver is a W3C standard. If you need to drive multiple browser engines through one protocol, Selenium's abstraction is the point.
  • Legacy suites. A large, working Selenium suite is an asset. Rewriting it introduces risk without a guaranteed reliability gain.

Selenium 4's CDP support means you can attach to a remote Chromium session and issue DevTools commands. It works. It is just more ceremony than connectOverCDP.

Where Playwright Pulls Ahead on Hosted Chromium

When the target is a hosted Chromium session, several Playwright features compound:

Auto-waiting. Playwright waits for elements to be actionable before clicking or typing. On a remote session with variable network latency, this removes a whole category of race conditions that Selenium users handle with explicit waits.

Browser contexts. Each context is an isolated cookie/storage jar inside one browser process. For agents that run many tasks, contexts give you cheap isolation without spinning up a new browser per task. That maps directly onto how hosted runtimes meter sessions.

Network routing. Playwright's page.route() lets you intercept, modify, or block requests. For agents that need to stub APIs or block trackers, this is a first-class capability rather than a CDP workaround.

Async by default. The TypeScript and Python APIs are async-native, which fits agent loops that interleave model calls and browser actions.

If you are building AI agents rather than test suites, the remote browser for AI agents guide covers how these features interact with agent runtimes.

Production Criteria Beyond the Library Choice

The library is one variable. The runtime is the other, and it often matters more. When you evaluate a hosted browser for either Selenium or Playwright, check:

  1. CDP endpoint availability. Does the runtime expose a WebSocket CDP endpoint you can pass to connectOverCDP? If it only offers a proprietary API, Playwright's native path is closed.
  2. Session isolation. Can you run concurrent sessions without cross-contamination of cookies, storage, or network state?
  3. Persistent profiles. Can a session keep logins across runs, or do you re-authenticate every time?
  4. Proxy and browser settings. Are proxy configuration and browser-level settings configurable per session? Prefer providers that document exactly which browser settings you can configure per session.
  5. Live debugging. Can you watch a session in real time when an agent gets stuck? A live viewer turns a 40-minute debugging session into a 2-minute one.
  6. Usage controls. How is time metered, and can you cap it? See pricing for current details rather than relying on secondhand numbers.

These criteria apply whether you write Selenium or Playwright. A great library on a flaky runtime still produces flaky automation.

Migrating Selenium Code to Playwright on a Remote Runtime

If you decide to move, the migration is mostly mechanical but has traps. A few patterns translate cleanly:

  • driver.get(url) → page.goto(url)
  • driver.findElement(By.css(...)) → page.locator(...)
  • Explicit waits → rely on auto-waiting, add expect() assertions where needed
  • driver.quit() → decide between browser.close() and disconnecting, based on session ownership

The trap is implicit waits. Selenium code often carries implicitlyWait calls that mask timing assumptions. Playwright has no implicit wait — it waits per action. Remove the old waits rather than porting them, or you will double up on timeouts.

For teams running both libraries during a transition, a hosted runtime that accepts CDP connections lets Selenium 4 and Playwright share the same session infrastructure. That reduces the blast radius of a gradual migration.

A Decision Checklist

Use this to settle the question for your project:

  • New project, Chromium target, hosted runtime? → Playwright.
  • Existing Selenium Grid with working CI? → Stay on Selenium unless reliability is a measured problem.
  • Need Ruby or PHP bindings? → Selenium.
  • Building AI agents with async loops? → Playwright.
  • Need to drive Firefox and WebKit through one protocol? → Selenium (or Playwright's own multi-browser support, which is separate from CDP).
  • Need live session debugging and persistent profiles? → Either library, but pick a runtime that provides them.

The library choice is reversible. The runtime choice is stickier, because it determines your connection model, your isolation guarantees, and your cost structure. Get the runtime right and either library will serve you; get it wrong and neither will.

Bottom Line

For remote Chromium sessions driven over CDP, Playwright is the better default in 2026: native connection, async APIs, auto-waiting, and cheap context isolation. Selenium remains correct for broad language support, existing grids, and standards-based multi-browser abstraction. Both can attach to a hosted runtime — the difference is how much glue code you write and how many timing bugs you debug.

If you want to test the Playwright path against a real hosted session, start with the remote browser online walkthrough or read the documentation for CDP connection details. For the underlying protocol, the Chrome DevTools Protocol documentation is the authoritative reference.