The Locator Keeps Changing on Every Refresh

Root Cause

The application is using dynamic attributes such as random IDs, auto-generated class names, or attributes that change every time the page loads. This is common in applications built using React, Angular, or Vue.

A beginner usually keeps updating the locator every time it changes, whereas an experienced automation tester first identifies why the locator changes and then chooses a stable locator strategy.

What to Do

  • Avoid using dynamic IDs, Names, or Classes if they change on every refresh.
  • Look for stable attributes such as:
    • placeholder
    • title
    • aria-label
    • Fixed class names
    • data-test
    • data-testid
    • data-qa
    • data-id
  • Use Relative XPath by identifying:
    • Parent elements
    • Static text
    • Nearby labels
    • following-sibling
    • preceding-sibling
    • Ancestor and descendant relationships

Interview Follow-up Questions

What if no attribute is stable?

Advertisement

Locate the element using nearby static text or neighboring elements.

What if the text also changes?

Use the element's structural relationship with stable parent or sibling elements.

How do you maintain locators in a large automation framework?

Centralize locators using the Page Object Model (POM).


No ID, No Name, and the XPath Keeps Changing

Root Cause

The application generates its DOM dynamically using frameworks such as React, Angular, or Vue.

A professional tester understands that if direct locators are unreliable, indirect stable anchors should be used.

Complete Strategy

  • Check whether automation-friendly attributes exist:
    • data-test
    • data-testid
    • data-qa
    • data-id
    • role
    • aria-label
    • aria-labelledby
  • Use text-based locators whenever visible text, placeholders, labels, or tooltips remain constant.
  • Locate a stable parent or ancestor element and navigate to the required element using Relative XPath.

Interview Follow-up Questions

  • What if the element has no text?
  • What if it is inside a Shadow DOM?
  • What if it is inside an iframe?
  • What if React completely re-renders the DOM?
  • Are Absolute XPaths recommended?
  • Is index-based XPath a good practice?

Element Not Clickable at Point

Root Cause

Selenium attempts to click an element while another element is covering it or before it becomes ready for interaction.

Common Causes

  • Sticky headers
  • Banners
  • Advertisements
  • Popups
  • Loaders
  • Error messages
  • Hidden or partially visible elements
  • Elements outside the viewport
  • Page still loading
  • Incorrect locator

Solution

  • Wait until the element becomes clickable using ExpectedConditions.elementToBeClickable().
  • Scroll the element into view before clicking.
  • Close or handle popups and overlays.
  • Use a more accurate locator.
  • Wait until page animations complete.
  • Check whether sticky headers overlap the element.
  • Report unstable UI behavior if it is an application defect.

StaleElementReferenceException

Interview Answer

"StaleElementReferenceException occurs when the WebElement located earlier is no longer attached to the DOM at the time Selenium interacts with it. This usually happens after a page refresh, AJAX update, DOM re-rendering, or navigation. I fix it by locating the element again, using explicit waits, avoiding storing WebElements for a long time, and handling dynamic page updates properly."

When It Happens

  • Page refresh
  • AJAX updates
  • DOM refresh
  • JavaScript recreates the element
  • Switching browser tabs or windows
  • Using an old WebElement reference

Real Project Example

"In one of my e-commerce projects, the product list refreshed after applying filters. Selenium had already stored the old WebElement. Once the DOM refreshed, Selenium tried to interact with the old element reference, resulting in StaleElementReferenceException."

Solution

  • Re-locate the element before interacting.
  • Use ExpectedConditions.refreshed().
  • Avoid caching WebElements inside Page Objects.

A Random Popup Breaks the Automation

Interview Answer

"Random popups usually appear because of announcements, advertisements, cookie banners, survey forms, or application modals. Instead of handling them individually every time, I create reusable popup handling methods that first check whether the popup exists and then close it safely before continuing the test execution."

Common Reasons

  • Cookie consent banners
  • Promotional popups
  • Discount offers
  • App download popups
  • Survey forms
  • Browser notification dialogs
  • React or Angular modals
  • Lazy-loaded components

These usually cause ElementClickInterceptedException because Selenium attempts to click elements behind the popup.

Solution

  • Identify all possible popups in the application.
  • Create a reusable popup handling utility that detects and closes known popups before important interactions.

Passes Locally, Fails in the CI/CD Pipeline

Root Cause

The test behaves differently because the execution environments are different.

Local Environment

  • Interactive browser
  • Normal execution speed
  • Stable network
  • User-controlled execution

CI/CD Environment

  • Headless browser
  • Faster execution
  • Parallel execution
  • Limited CPU and memory
  • Different browser resolution

Common Causes

  • Timing issues
  • Environment differences
  • Viewport differences
  • Test data differences
  • Cache or cookies unavailable

Solution

  • Replace hardcoded waits with explicit waits.
  • Execute tests locally in headless mode.
  • Ensure independent test data.
  • Set browser window size explicitly.
  • Capture screenshots and logs whenever failures occur in CI.

The Page Loads Slowly

Common Causes

  • Large images
  • Heavy UI components
  • Multiple backend API calls
  • Slow database queries
  • Third-party service delays
  • Network latency
  • Poor frontend optimization
  • Missing caching
  • Slow QA or staging environment

The first step is identifying whether the issue belongs to the frontend, backend, or environment.

Investigation Steps

  • Reproduce consistently.
  • Test on multiple browsers and environments.
  • Use Browser DevTools Network tab.
  • Check API response times.
  • Review server logs if available.

Automation Solution

  • Use WebDriverWait.
  • Verify document.readyState.
  • Increase timeout values where necessary.
  • Implement retry logic when appropriate.

Reporting Details

Include:

  • Steps to reproduce
  • Environment
  • Browser version
  • Timestamps
  • HAR logs
  • Expected load time
  • Actual load time

Useful Tools


Dynamic (AJAX-Loaded) Dropdowns

Modern applications frequently populate dropdown values through AJAX requests or JavaScript events.

Automation fails when Selenium tries to select an option before the list has loaded.

Solution

  • Use Explicit Waits until options become visible.
  • Use dynamic XPath or CSS selectors.
  • Type into searchable dropdowns using sendKeys() to trigger AJAX loading.
  • Retry selection if intermittent failures occur.

Nested iFrames

Real Project Example (STAR)

Situation

The payment page contained a credit card field inside a child iframe, which itself was inside another parent iframe.

Task

Interact successfully with elements inside nested iframes.

Action

"I first switched to the parent iframe using driver.switchTo().frame(), then switched to the child iframe, located the required element, completed the action, and finally switched back to the default content. I also added explicit waits before switching to each frame."

Result

The automation consistently completed the payment flow without failures.

Standard Approach

Switch to Parent Frame

Switch to Child Frame

Perform the Required Action

Return to defaultContent()

Always use explicit waits while switching between frames.


FAQs

What should you do when a locator changes on every refresh?

Avoid dynamic attributes and locate elements using stable attributes such as placeholder, aria-label, title, or data-test attributes. If necessary, use Relative XPath based on parent or sibling relationships.


What causes "Element Not Clickable at Point"?

It is usually caused by overlapping elements, popups, sticky headers, page loading issues, elements outside the viewport, or incorrect locators.


What is StaleElementReferenceException?

It occurs when Selenium tries to interact with a WebElement that is no longer attached to the DOM after a page refresh, AJAX update, or DOM re-rendering.


Why does a Selenium test pass locally but fail in CI/CD?

Differences in execution speed, headless browsers, viewport sizes, system resources, and test data commonly lead to failures in CI/CD environments.


How do you handle random popups?

Identify all expected popups in the application and create a reusable method that detects and closes them before important interactions.


How do you handle nested iframes?

Switch to the parent frame, then the child frame, perform the required action, and finally return to the default content using defaultContent().


How do you automate AJAX dropdowns?

Wait until options load completely, use dynamic locators, trigger searches using sendKeys() where applicable, and implement retry logic for unstable loading.