Back to cheat sheets

Test Frameworks

Selenium

Architecture, locators, the three wait types, stale elements, Page Object Model, frames/windows/alerts, Grid, and how Selenium contrasts with Playwright — with rapid-fire Q&A.

01Architecture

Selenium WebDriver drives real browsers through a browser-specific driver (chromedriver, geckodriver). Your test code sends commands over the W3C WebDriver protocol (HTTP/JSON) to the driver, which translates them into native browser actions.

Client → WebDriver protocol → browser driver → browser. Each command is a separate HTTP round trip — the root of much of the latency and flakiness you manage with waits.

Selenium 4 standardized on the W3C protocol and added relative locators and CDP access.

02Locators

Eight strategies, in rough order of preference:

  • id — fastest, most stable when present.
  • name, className, tagName.
  • linkText, partialLinkText.
  • cssSelector — fast, flexible; generally preferred over XPath.
  • xpath — most powerful (can traverse up the DOM, match text) but slower and more brittle.
CSS vs XPath: prefer CSS for speed and readability; reach for XPath only when you need text matching or to walk to a parent/ancestor.

03The Three Wait Types

The most important Selenium interview topic — the heart of flakiness control.

Implicit Wait

A global setting: poll the DOM up to N seconds when finding any element. Simple but blunt — applies everywhere and can mask issues.

Explicit Wait

WebDriverWait + ExpectedConditions — wait for a specific condition (element clickable, visible, present) on a specific element. The recommended approach.

Fluent Wait

An explicit wait with a custom polling interval and ignored exception types — fine-grained control.

Warning: never mix implicit and explicit waits — the combination produces unpredictable cumulative timeouts. Pick explicit. And avoid Thread.sleep() entirely.
new WebDriverWait(driver, Duration.ofSeconds(10))
    .until(ExpectedConditions.elementToBeClickable(By.id("submit")));

04StaleElementReferenceException

Thrown when a previously found element reference is no longer attached to the DOM — typically after the page re-rendered or navigated. Causes and fixes:

  • Re-find the element right before interacting instead of caching it.
  • Wait for the new element after a DOM change.
  • This is exactly the problem Playwright's lazy locators avoid by re-querying automatically.

05Frames, Windows & Alerts

  • iframesswitchTo().frame(...) before interacting; defaultContent() to return.
  • Windows/tabsgetWindowHandles() + switchTo().window(handle).
  • AlertsswitchTo().alert() then accept()/dismiss().
  • Actions API for hover, drag-drop, key chords.

06Page Object Model & Grid

POM — one class per page encapsulating locators and actions, so UI changes touch one file. PageFactory (@FindBy) is the classic Selenium helper.

Selenium Grid — distribute tests across multiple machines/browsers via a hub-and-node (Selenium 4: router/distributor/node) model for parallel, cross-browser execution. Pairs with TestNG/JUnit parallel config.

07Selenium vs Playwright

AspectSelenium / Playwright
ProtocolW3C WebDriver (HTTP per command) / persistent CDP-style connection.
WaitingYou add explicit waits / auto-wait built into actions.
Network mockingLimited (CDP) / first-class.
SetupManage driver binaries / bundled browsers.
MaturityHuge ecosystem, any language/browser / newer, faster, fewer integrations.

Selenium's strengths: maturity, language breadth, real-browser fidelity, and broad grid/cloud support. Playwright's: speed, reliability, and built-in tooling.

08Rapid-Fire Q&A

Reveal each answer to self-check, then test yourself with the quiz.

Implicit vs explicit wait?

Implicit = global polling for all element lookups; explicit = wait for a specific condition on a specific element. Prefer explicit; never mix them.

Why avoid Thread.sleep?

It's a fixed blind wait — slow when too long, flaky when too short; explicit waits react to actual state.

What causes StaleElementReferenceException?

The cached element detached from the DOM after a re-render/navigation; re-find it before interacting.

CSS selector vs XPath?

CSS is faster and cleaner; XPath can match text and traverse to parents/ancestors but is slower and more brittle.

How do you handle an iframe?

switchTo().frame(...) before interacting, then switchTo().defaultContent() to leave.

How does Selenium talk to the browser?

Test code sends W3C WebDriver commands over HTTP to a browser-specific driver, which performs native actions.

What is Selenium Grid?

A hub/node setup to run tests in parallel across multiple machines, browsers, and OSes.

How do you reduce flakiness?

Explicit waits, stable locators (id/css), re-finding elements, no sleeps, and test independence.