Recipes
One problem, one working solution, four languages. Each recipe explains the trade-offs and links to the lesson that covers the theory.
Elements & Locators
Retry on StaleElementReferenceException
Re-find an element and retry the action when the DOM re-renders between locating and using it.
Find Elements in Nested Shadow DOM
Walk through several shadow roots to reach an element inside deeply nested web components.
Check Whether an Element Exists Without an Exception
Test for presence, visibility or absence of an element cleanly, without try/catch and without waiting the implicit timeout.
Get the Text of a Hidden Element
Read text that getText() returns empty for: collapsed sections, off-screen items, and elements with display:none.
Interact With an Element Inside an iFrame and Return
Switch into a frame by element, act, and switch back safely, with a helper that guarantees the return even when an assertion fails.
Find a Table Row by Cell Text and Act on It
Locate the row that contains a given value, then read or click another cell in the same row, using a header lookup instead of hard-coded column numbers.
Waits & Timing
Wait for a Loading Spinner to Disappear
Wait for an overlay or spinner to be gone before interacting, including the case where it never appeared.
Wait for Text to Change and Stabilise
Wait until an element's text differs from its previous value and stops changing, for counters, totals and live-updating fields.
Wait for Network Idle (No Pending Fetch or XHR)
Wait until the page has no in-flight fetch or XHR requests for a quiet period, using a small injected tracker that works in every browser and binding.
Wait for a CSS Animation or Transition to Finish
Wait until a sliding drawer, modal or accordion has stopped moving before clicking, using the Web Animations API with a position-stability fallback.
Wait for a Popup Window That Opens Late
A reusable wait that returns the handle of a window opened asynchronously (OAuth, payment, print preview), with a timeout and clean switching back.
Interactions
Click With a JavaScript Fallback
Try a real click first, then fall back to a JavaScript click only for the specific interception and visibility errors, with a log line so fallbacks are visible.
Hover a Menu and Click a Submenu Item
Open a hover-driven dropdown with the Actions API and click an item that only exists while the pointer is over the trigger.
Select a Dropdown Option by Visible Text
Choose options in native select elements with the Select helper, and handle custom (non-select) dropdowns built from divs.
Set a Date in a Date Picker
Type into the input when allowed, set the value directly when it is read-only, or drive the calendar widget when nothing else works.
Clear and Type Into a React or Angular Input Reliably
Replace the value of a framework-controlled input when clear() does not stick, using select-all plus type and the native setter fallback.
Handle an Unexpected Alert Without Failing
Configure the session to dismiss surprise alerts automatically, and accept or dismiss expected ones explicitly with a wait.
Set a Checkbox to a Specific State (Not Just Toggle It)
Make a checkbox checked or unchecked idempotently, including hidden native inputs behind styled labels and ARIA switches that use aria-checked.
Load Every Item on an Infinite Scroll Page
Scroll a lazy-loading feed until no new items arrive, with a hard cap, a growth-based wait instead of sleeps, and support for scrollable containers.
Drag and Drop That Works on HTML5 draggable Elements
When Actions.dragAndDrop moves the mouse but nothing drops, dispatch the HTML5 drag events with a shared DataTransfer object from JavaScript instead.
Select Multiple Options in a Multi-Select List
Pick several options in a select element with the multiple attribute, clear previous choices, read back what is selected, and handle custom listboxes.
Copy and Paste Text with Keyboard Shortcuts
Copy text from one field and paste it into another with Ctrl or Cmd shortcuts, pick the right modifier per OS, and test custom paste handlers reliably.
Scroll an Element to the Centre of the Viewport
Avoid clicks blocked by sticky headers and cookie bars by scrolling the target to the middle of the screen, with instant behaviour so the next click is safe.
Browser & Session
Click a Link That Opens a New Tab and Switch to It
Detect the new window handle after a click, switch to it, do work, and return to the original tab without guessing handle order.
Highlight an Element for Debugging and Screenshots
Draw a temporary outline around the element a step is about to use so screenshots and videos show what the test targeted.
Set the Download Directory and Suppress Prompts
Configure Chrome, Firefox and Edge to save downloads silently into a directory the test controls, including PDFs that would otherwise open inline.
Dismiss a Cookie Consent Banner Once
Skip the consent banner in every test by pre-setting the cookie or storage key it checks, with a click fallback.
Fail a Test on Browser Console Errors
Collect console.error calls and uncaught JavaScript exceptions with WebDriver BiDi, then fail the test with the messages, while ignoring known noise.
Emulate a Mobile Device in Chrome and Edge
Run a test at phone size with a mobile user agent, device pixel ratio and touch support using Chrome's mobileEmulation option, plus a Firefox fallback.
Fake the Browser's Geolocation
Make navigator.geolocation report chosen coordinates: grant the permission up front, then override the location with WebDriver BiDi or Chrome DevTools.
Install a Browser Extension in a Test Session
Load a Firefox add-on at runtime or a Chrome extension at startup, find its ID, uninstall it again, and know the limits of branded Chrome builds.
Files & Media
Wait for a File Download to Complete
Configure a download directory, trigger the download, and wait until the file exists and is no longer a partial file.
Upload a File Locally and Through a Grid
Send a file path to an input[type=file], make it work on a remote Grid or cloud with LocalFileDetector, and handle hidden file inputs behind styled buttons.
Screenshot a Single Element to a File
Capture just one component, named by test and timestamp, ready for a visual comparison tool or a bug report.
Verify the Contents of a Downloaded File
Assert on a downloaded CSV, PDF or Excel file: read it after download completes, check headers and rows, and clean up.
Take a Full-Page Screenshot (Beyond the Viewport)
Capture the entire scrollable page, not just the visible viewport: native in Firefox, a CDP command in Chrome and Edge, and why stitching scrolled images fails.
Network & Auth
Pass HTTP Basic Authentication
Get through a browser's basic auth prompt with BiDi's authentication handler, and the URL-embedded fallback for older setups.
Block Analytics and Third-Party Requests
Stop trackers, chat widgets and ad scripts from loading during tests to speed up pages and remove external flakiness.
Mock an API Response
Return a canned JSON payload for one endpoint so the UI shows empty, error or edge-case states without changing the backend.
Log In Once via the API and Reuse the Session Cookie
Skip the login form in every test: authenticate over HTTP, inject the session cookie into the browser, and land on an authenticated page in under a second.
Test Structure
Take a Screenshot on Test Failure Automatically
A framework hook for JUnit 5, pytest, Mocha and NUnit that saves a screenshot and page source only when a test fails.
Soft Assertions: Check Several Things, Report All Failures
Verify multiple fields on one page and report every mismatch at once instead of stopping at the first.
Pick the Browser, Headless Mode and Grid From Environment Variables
Run the same suite on Chrome, Firefox or Edge, locally or on a Grid, headed or headless, by changing environment variables instead of code.
Run an axe Accessibility Scan in a Selenium Test
Scan a page with axe-core from inside an existing Selenium test, fail only on serious WCAG violations, and print readable results with selectors.
Find Broken Links on a Page
Collect every link on a page in one script call, check each URL with a fast HTTP HEAD request in parallel, and report 4xx and 5xx responses with their text.