Playwright vs Selenium in 2021

For a decade, Selenium WebDriver was the undisputed king of browser automation. But Microsoft’s Playwright has matured rapidly and provides a compelling alternative. Having migrated a massive E2E test suite from Selenium to Playwright, here is my detailed comparison.

Architecture Comparison

Selenium uses the WebDriver HTTP protocol. It sends an HTTP request for every action (Click, Type). This introduces latency and flakiness.

Playwright uses the browser’s DevTools Protocol (CDP) via a WebSocket. It is bi-directional and event-driven.

FeatureSeleniumPlaywright
CommunicationHTTP (Slow)WebSocket (Fast)
WaitingManual (Implicit/Explicit Waits)Automatic Waiting
ParallelismGrid (Complex)Browser Contexts (Native)
Network ControlLimitedFull Mocking/Interception

The Killer Feature: Auto-Waiting

In Selenium, `ElementClickInterceptedException` is the stuff of nightmares. In Playwright, `page.ClickAsync()` automatically waits for:

  • Element to be attached to DOM
  • Element to be visible
  • Element to be stable (not animating)
  • Element to receive events (not obscured)

Code Comparison

// Playwright .NET
using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Chromium.LaunchAsync();
var page = await browser.NewPageAsync();

// Network Interception!
await page.RouteAsync("**/*.png", route => route.AbortAsync()); // Block images for speed

await page.GotoAsync("https://example.com");
await page.ClickAsync("text=Get Started"); // Text selector engine
var title = await page.TitleAsync();

The resulting tests are 3x faster and significantly less flaky. The migration effort is non-trivial (syntax is different), but the long-term ROI on maintenance is massive.


Discover more from C4: Container, Code, Cloud & Context

Subscribe to get the latest posts sent to your email.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.