Dynamic locators are the most common reason Selenium tests break: ids that change on every load, generated class names, elements without text, rows that move. These Selenium dynamic locator scenario questions show how to find stable locators in each case, with the XPath and CSS patterns that work, and how to stop the problem at its source.

Patterns for Dynamic Attributes

SituationXPathCSS
Stable prefix//input[starts-with(@id,'user_')]input[id^='user_']
Stable fragment anywhere//div[contains(@class,'error')]div[class*='error']
Stable suffix//a[substring(@href, string-length(@href)-3)='.pdf']a[href$='.pdf']
Visible text//button[normalize-space()='Save']Not possible in CSS
From a label//label[.='Email']/following-sibling::inputlabel + input (adjacent only)
Multiple conditions//input[@type='text' and @name='q']input[type='text'][name='q']

1. The locator changes every time the page refreshes. What will you do?

Find the part that doesn't change. Dynamic ids are often a stable prefix plus a random suffix (user_8f3a2), so use starts-with(@id,'user_') or CSS [id^='user_']. Otherwise anchor on other stable attributes (name, type, aria-label, a data-test attribute) or on visible text, and wait for the element explicitly.

2. The element has no id or name, and the XPath keeps changing. How do you identify it?

Combine attributes (//button[@type='submit' and contains(@class,'primary')]), use the label or nearby text (//label[normalize-space()='Email']/following-sibling::input), anchor on a stable container and go down from it, or use Selenium 4 relative locators. Avoid indexes unless the order is fixed.

3. What if the element has no text either?

Use attributes such as placeholder, title, aria-label, type or src, the element's position relative to a stable labelled element (axes or relative locators), or its role in a table row identified by another cell's text.

4. What if no attribute is stable at all?

Use structure relative to something stable (a heading, a label, a table header) and keep the locator short. Then fix the root cause: ask developers to add data-testid attributes, which is cheap for them and removes the problem permanently.

5. How do you identify which attributes are dynamic?

Refresh the page or open it in a new session several times and compare the element in DevTools; values that change, or look generated (long hashes, numbers, framework class names such as css-1x2y3z), are dynamic.

6. A locator fails randomly during execution. What do you check?

Whether it matches more than one element; whether the element is re-rendered (StaleElementReferenceException); timing (use explicit waits for visibility or clickability, never Thread.sleep); overlays and animations; and frames or shadow DOM. Run the test repeatedly and capture screenshots and page source on failure.

7. How do you handle elements in dynamic tables and lists?

Locate rows by content instead of position: //tr[td[normalize-space()='Order #1042']]//button[.='View']. Iterate with findElements and filter in Java when the logic is complex.

8. How do you avoid StaleElementReferenceException with dynamic content?

Re-find the element after actions that re-render the page, wait for the old element to go stale (ExpectedConditions.stalenessOf) and then for the new one, and keep By locators rather than stored WebElements in page objects.

9. How do you maintain locators in a large project?

Keep them in page objects (one place per page), name them by purpose, review new locators in code review, prefer test ids and accessible attributes, and run a quick locator-health check in CI so breakage shows up early.

10. Should manual testers care about locators?

Yes: noticing missing or generated ids during manual testing and asking for test ids early makes automation much cheaper. Manual testers also use locators when inspecting issues and writing precise bug reports.

Advertisement

FAQs

How do you write a dynamic XPath?

Match only the stable part of an attribute with contains() or starts-with(), combine several conditions, or anchor on nearby stable text and move with axes such as following-sibling or ancestor.