Selenium 4 Locator Strategies: Stable Elements, Relative Locators and Shadow DOM

Locators connect a Selenium test to the web interface. A locator that depends on generated classes or deep page structure may break during a harmless design change. A strong locator identifies the intended element uniquely, communicates meaning and remains stable across releases.

Selenium 4 adds useful capabilities, but fundamental selector discipline is still the best defence against fragile tests.

Prefer dedicated stable attributes

Unique IDs are effective when the application guarantees stability. If IDs are generated or reused, ask developers for dedicated attributes such as data-testid or another agreed convention.

Test attributes should describe purpose, not screen position. checkout-submit is more durable than right-column-button-2. Keep the convention documented and treat breaking changes like an interface change.

Use accessible attributes when appropriate

Labels, names and roles can create readable locators and encourage accessible design. Link text works well when the text is stable and unambiguous. Localised or frequently edited copy may be a weak dependency.

Do not use an accessibility attribute solely as a test hook if that makes the accessibility information inaccurate.

Choose CSS or XPath by need

CSS selectors are concise for IDs, classes, attributes and hierarchical relationships. XPath can navigate by text and express relationships that CSS cannot easily represent.

Avoid absolute XPath from the document root and selectors with many div levels. They encode presentation structure rather than element identity. Keep either strategy short and scoped to a stable container.

Understand relative locators

Selenium 4 relative locators can find an element above, below, near, to the left of or to the right of another element. They can help when the page lacks stable identifiers but has a clear visual relationship.

Layout changes can alter relative position, and “near” may match an unexpected control. Use relative locators selectively, combine them with a base type or attribute and verify uniqueness.

Work with dynamic lists

For repeated rows or cards, locate the stable container first, then search within it. Identify a row by a business value and find its action button relative to that row.

Avoid selecting the third button unless the position itself is the requirement. Sorting or filtered results will change indexes.

Access Shadow DOM

Web components can encapsulate elements inside a shadow root. Selenium 4 provides supported shadow-root access for open shadow DOM. Locate the host, obtain its shadow root and then locate elements inside that context.

Closed shadow roots are intentionally inaccessible through normal means. Do not build brittle script-based workarounds without discussing testability with the development team.

Handle iframes separately

An element inside an iframe is not in the current browsing context. Wait for the frame, switch into it and then locate the element. Return to the parent or default content when finished.

A perfect selector fails if the driver is in the wrong frame or window, so include context in troubleshooting.

Validate locator quality

Check that the locator matches exactly one intended element in the required state. Run it against realistic variations: empty data, multiple rows, validation errors and responsive layouts. Log the locator and relevant page state when an element cannot be found.

Centralise locators in page or component objects, but do not reuse one vague selector for unrelated controls.

Team practice exercise

Take a brittle test with absolute XPath, index selectors and text dependencies. Replace them with stable attributes and scoped container searches. Add one relative-locator example and one open-shadow-root example, then describe when each could fail.

Learn modern browser automation in the Selenium course in Vizag. Organise selectors with the Page Object Model and validate them at scale using Selenium Grid parallel testing.

Final takeaway

Stable locators depend on meaning, uniqueness and controlled scope. Prefer intentional attributes, use CSS or XPath according to the relationship, and treat relative locators and shadow-root access as specialised tools rather than universal fixes.