Skip to main content
SeleniumDecoded

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

Waits & Timing

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.

Medium #click#javascript-executor#intercepted

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.

Medium #hover#actions#menu

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.

Stable #select#dropdown#options

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.

Medium #date-picker#calendar#input

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.

Stable #react#angular#vue

Handle an Unexpected Alert Without Failing

Configure the session to dismiss surprise alerts automatically, and accept or dismiss expected ones explicitly with a wait.

Stable #alerts#unhandled-prompt#capabilities

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.

Stable #checkbox#toggle#switch

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.

Medium #scrolling#infinite-scroll#lazy-loading

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.

Fragile #drag-and-drop#html5#javascript

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.

Stable #select#multi-select#listbox

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.

Medium #clipboard#keyboard#copy-paste

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.

Stable #scrolling#sticky-header#click-intercepted

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.

Stable #windows#tabs#window-handles

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.

Stable #debugging#screenshots#javascript-executor

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.

Stable #download#chrome-options#firefox-preferences

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.

Medium #cookies#consent#gdpr

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.

Stable #console#bidi#logging

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.

Stable #mobile#emulation#responsive

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.

Medium #geolocation#bidi#cdp

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.

Medium #extensions#add-ons#firefox

Files & Media

Network & Auth

Test Structure