Selenium Scenario Based Interview Questions
Experienced Selenium interviews rarely ask "what is XPath?". They describe a situation — "the locator changes on every refresh", "the test passes locally but fails in Jenkins" — and listen for how you diagnose it. For each scenario below you'll find the root cause, the fix with Selenium 4 code, and the follow-up questions interviewers ask next.
How to Answer a Scenario Question
- Name the root cause — not just the symptom.
- Give the fix — with the code or technique.
- Prevent it — how your framework stops it happening again.
- Add a real example — a sentence from your own project, if you have one.
1. The Locator Changes on Every Refresh
Root cause
The application generates dynamic attributes — random IDs like btn-8f3a2c, hashed class names from CSS-in-JS, or indexes that shift. Updating the locator every time is the beginner's answer; anchoring to something stable is the experienced one.
Fix
// Stable test attribute — ask developers to add these
By email = By.cssSelector("[data-testid='login-email']");
// Stable part of a dynamic attribute
By cancelButtons = By.xpath("//button[starts-with(@id,'btn-')]");
By emailByName = By.xpath("//input[contains(@name,'email')]");
// Stable visible text or a nearby label
By emailByLabel = By.xpath("//label[text()='Email']/following-sibling::input");
// A stable neighbour: the Cancel button for order #1044
By cancel1044 = By.xpath("//h3[normalize-space()='Order #1044']/ancestor::div[@class='card']//button");
Follow-ups
What if no attribute is stable? Anchor to static text or a neighbouring element and navigate with XPath axes, as in the last example.
What if the text changes too (e.g. multiple languages)? Use structure relative to a stable container — and push for data-testid attributes, the real long-term fix.
How do you maintain locators at scale? Keep them in page objects so each one lives in one place. More: Locators & XPath and Absolute vs Relative XPath.
2. Element Not Clickable at Point
Root cause
Selenium clicks at the element's centre, but something else is there: a loading spinner, a cookie banner, a sticky header, an animation still running — or the element is outside the viewport. Selenium 4 usually reports this as ElementClickInterceptedException.
Fix
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector(".spinner"))); // overlay gone
WebElement button = driver.findElement(By.id("checkout"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView({block: 'center'});", button);
wait.until(ExpectedConditions.elementToBeClickable(button)).click();
Use a JavaScript click only as a last resort — it clicks even when a real user couldn't, so it can hide genuine bugs. Full guide: Element Not Clickable at Point.
3. StaleElementReferenceException
Interview answer
"A stale element is a WebElement I found earlier that's no longer attached to the page, because the DOM re-rendered — after a filter, a sort, an AJAX refresh or navigation. The fix is to find the element again after the change, store By locators instead of WebElements, and wait for the refresh to finish."
Real example
A product list re-renders after applying a filter; a list of product elements collected before the filter goes stale.
Fix
By productNames = By.cssSelector(".product .name");
driver.findElement(By.id("filter-laptops")).click();
WebElement first = driver.findElement(productNames);
new WebDriverWait(driver, Duration.ofSeconds(10))
.until(ExpectedConditions.stalenessOf(first)); // old list is gone — only if the list actually re-renders
List<String> names = driver.findElements(productNames) // find again, after the change
.stream().map(WebElement::getText).toList();
More: StaleElementReferenceException Fix.
4. A Random Popup Breaks the Test
Root cause
Cookie consent banners, newsletter or survey modals, chat widgets and promotions appear unpredictably and intercept clicks.
Fix — in order of preference
- Remove it in test environments: a feature flag, a cookie set before the test, or a config switch that disables promos in QA.
- Pre-set the consent cookie so the banner never appears:
driver.get(baseUrl);
driver.manage().addCookie(new Cookie("cookie_consent", "accepted"));
driver.navigate().refresh();
-
A small, reusable dismiss helper at known points (after login, on page load) — never a
try/catcharound every click:
public void dismissPopupIfShown() {
List<WebElement> close = driver.findElements(By.cssSelector("[data-testid='promo-close']"));
if (!close.isEmpty() && close.get(0).isDisplayed()) close.get(0).click();
}
findElements() returns an empty list instead of throwing, so the check doesn't wait or fail when the popup is absent (as long as the implicit wait is 0).
5. Passes Locally, Fails in CI
Root cause
The environments differ: headless mode with a small default window, slower shared agents, different browser versions, missing test data, time zone or locale, and tests running in parallel.
Fix
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless=new", "--window-size=1920,1080"); // same layout as a desktop screen
WebDriver driver = new ChromeDriver(options);
- Replace every fixed wait with an explicit wait — slow agents expose timing bugs.
- Create test data per test instead of relying on existing records.
- Capture a screenshot, page source and browser logs on failure, and archive them in the pipeline.
- Reproduce locally in headless mode with the CI window size before changing anything.
Full checklist: Test Passes Locally but Fails in Jenkins.
6. The Page Loads Slowly
First decide whose problem it is: frontend (large scripts, rendering), backend (slow API responses) or environment (an overloaded test server). Check the browser DevTools Network tab for slow requests and compare with other environments.
- In the test: wait for the specific element that proves the page is ready — not a longer global timeout.
- If it's genuinely slow: report it with evidence — the URL, timings, the slow request, the environment and how often it happens. Slowness is a defect, not something to hide with bigger timeouts.
- Tools: browser DevTools, Lighthouse, and a load-testing tool such as JMeter for performance under load.
7. AJAX-Loaded (Dynamic) Dropdowns
The options arrive after an API call, so selecting immediately fails. For a native <select>:
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
By cityOptions = By.cssSelector("#city option");
wait.until(ExpectedConditions.numberOfElementsToBeMoreThan(cityOptions, 1)); // more than the placeholder
new Select(driver.findElement(By.id("city"))).selectByVisibleText("Pune");
For a custom autocomplete, type a few characters, wait for the suggestion, then click it:
driver.findElement(By.id("city-search")).sendKeys("Pun");
wait.until(ExpectedConditions.elementToBeClickable(
By.xpath("//li[@role='option'][normalize-space()='Pune']"))).click();
8. Nested iFrames
STAR example
- Situation: the card-number field of a payment page sat in a child iframe inside another iframe.
- Task: fill the card details reliably.
- Action: waited for and switched into the outer frame, then the inner one, filled the field, then returned to the main page.
- Result: the payment flow ran consistently in every run.
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("payment-frame")));
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[title='Card number']")));
driver.findElement(By.name("cardnumber")).sendKeys("4111111111111111");
driver.switchTo().defaultContent(); // back to the main page
Frames are always found relative to the current one — you can't jump straight to the inner frame. More: Alerts, Frames & Windows.
Practise answering these out loud and under time pressure in the Selenium Interview Simulator.
From Real Projects
In my automation roles I created, modified and enhanced Selenium WebDriver scripts in Java, organised with POM classes and a business library, and ran them with TestNG on Chrome and Firefox. Exercises like these build exactly the muscle memory you need when a real application — for me, Canolog's sales, inventory and service screens — throws a new kind of element at you. Structure each answer as the problem, what you checked, and what you changed.
📚 Official documentation: Selenium documentation · ISTQB Glossary of testing terms
FAQs
What do you do when a locator changes on every refresh?
Anchor to something stable: a data-testid, the stable part of an attribute (starts-with, contains), visible text, or a stable neighbouring element via XPath axes.
What causes "element not clickable at point"?
Another element covers the target — an overlay, spinner, banner or sticky header — or the element is animating or off-screen. Wait for the overlay to disappear, scroll to the element, then wait until it's clickable.
What is StaleElementReferenceException?
A previously found element is no longer attached to the DOM because the page re-rendered. Find it again after the change and store By locators instead of WebElements.
Why does a test pass locally but fail in CI?
Headless window size, slower agents, browser versions, test data, time zone and parallel execution. Fix with explicit waits, a fixed window size, per-test data and failure evidence.
How do you handle random popups?
Disable them in test environments or pre-set their cookies; otherwise use a small helper that closes them at known points using findElements().
How do you handle nested iframes?
Switch into the outer frame, then the inner one (waiting for each), do the work, and return with switchTo().defaultContent().
Related
- Top 50 Selenium Interview Questions
- Top 25 Scenario-Based Testing Questions
- Selenium Errors & Exceptions Guide