Skip to content

Cheat sheet

Selenium WebDriver

Waits, locators, stale elements, and framework answers interviewers keep asking.

Waits

Implicit wait
A global polling time for finding elements. Avoid mixing it with explicit waits; the combined timeout becomes hard to predict.
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
Explicit wait
Waits for one condition on one element. The default choice.
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(by));
Fluent wait
Explicit wait with a custom poll interval and ignored exceptions.
Thread.sleep
A fixed delay. Slows every run and still flakes. Say you replace it with a condition.

Locators, best to worst

id / test id
Stable and fast when it is unique. Ask developers for data-testid.
CSS selector
Fast and readable. Prefer over XPath unless you need text or parent traversal.
button[data-testid='submit']
XPath
Powerful for text and axes; brittle when it uses absolute paths or indexes.
//tr[td[text()='Alice']]//button
Link text
Fine for simple links; breaks on copy changes or translation.

Common failures

StaleElementReferenceException
The element was re-rendered. Re-find it after the action instead of caching it.
ElementClickInterceptedException
Something covers the element (overlay, sticky header). Wait for it to go or scroll into view.
NoSuchElementException
Wrong locator, element not loaded yet, or it is inside an iframe or shadow DOM.

Switching context

Iframe
Switch in, act, switch back.
driver.switchTo().frame("id"); driver.switchTo().defaultContent();
Window or tab
Store the original handle, loop handles to find the new one.
Alert
switchTo().alert() then accept(), dismiss(), or getText().

Framework answers

Page Object Model
One class per page or component holding locators and actions. Tests read as business steps; a UI change touches one file.
Parallel runs
One driver per thread (ThreadLocal), no shared mutable data, independent tests.
Selenium Grid
Runs tests on many browsers and machines in parallel through a hub and nodes.