What We Are Learn:
Toggle1️⃣ Using Robust and Dynamic Locators
I avoid absolute XPath and static IDs as much as possible.
What I use instead:
- Relative XPath
contains(),starts-with()- Stable attributes like
data-testid,aria-label - Parent–child relationships
Real‑time example:
In one Angular app, button IDs were generated dynamically on every refresh. I used:
//button[contains(text(),’Submit’)]
instead of ID‑based locators.
2️⃣ Explicit Waits (Most Important Technique)
Dynamic elements often appear after an API call or JS execution, so timing is critical.
I use:
WebDriverWaitExpectedConditionslike:visibilityOfElementLocatedelementToBeClickablepresenceOfElementLocated
Example:
A search result list appeared only after backend response. Without wait, scripts failed intermittently.
✅ Replacing Thread.sleep() with explicit waits reduced flaky failures significantly.
3️⃣ Handling Stale Element Reference Exception
Dynamic pages often refresh the DOM, making previously located elements invalid.
How I handle it:
- Re‑locate the element before interacting
- Wrap actions in retry logic
- Use
ExpectedConditions.refreshed()
Real‑time scenario:
After clicking “Save”, the page refreshed partially and old references broke. Re‑fetching elements solved it.
4️⃣ Handling Dynamic Dropdowns and Auto‑Suggestions
For AJAX‑based dropdowns:
- Wait until options count > 1
- Fetch list dynamically
- Iterate and select based on visible text
Example:
State → City dropdown where cities load dynamically after state selection.
5️⃣ Using JavaScriptExecutor (When Selenium Fails)
Some dynamic elements are:
- Hidden
- Overlapped
- Not interactable via normal click
In such cases, I use:
js.executeScript(“arguments[0].click();”, element);
Used mainly as a last resort, not default.
6️⃣ Page Object Model (POM) for Stability
I always implement POM, so when a dynamic locator changes:
- Fix happens in one place
- No impact on test logic
This is critical in large regression suites.
Selenium 4 no longer uses JSON Wire Protocol because it was deprecated and non‑standard. Selenium 4 fully adopts the W3C WebDriver standard, eliminating protocol translation, improving cross‑browser consistency, and enabling modern browser features.
Thread.sleep() is a Java hard wait that pauses execution for a fixed time, whereas setSpeed() was used only in Selenium IDE to slow down execution. In Selenium WebDriver, setSpeed() does not exist, and explicit waits are the recommended approach.
What is POM?
Page Object Model is a design pattern, not a Selenium class.
In POM:
- Each web page is represented by a separate Java class
- Page class contains:
- Web element locators
- Business methods (actions)
POM is used to separate test logic from UI locators, improving maintainability and reusability.
What is Page Factory?
Page Factory is a Selenium‑provided class (org.openqa.selenium.support.PageFactory) that supports the POM design pattern.
It:
- Uses
@FindByannotations - Initializes elements using
PageFactory.initElements() - Implements lazy loading for elements
Page Factory is a helper mechanism, not a design pattern itself.