Choosing Between Selenium 4 Relative Locators and Traditional Locators
A decision guide that compares Selenium 4 relative locators with traditional locators, outlines constraints, trade‑offs, and provides a Java example with verification steps.
05 Jan 2026, 20:37 UTC

Decision and Constraints
When automating a web UI test you need to locate an element that does not have a stable unique attribute (such as an id or name). In this situation you can either fall back to a complex XPath/CSS selector or use Selenium 4’s relative locators, which find an element based on its visual relationship to another element that is uniquely locatable.
Constraints for using relative locators:
- Selenium version 4.0.0 or later (the
RelativeLocatorclass is unavailable in earlier releases). - A W3C‑compliant WebDriver implementation (ChromeDriver 88+, GeckoDriver 0.26+, etc.).
- The reference element used for the relative positioning must be present in the DOM and uniquely identifiable by a traditional locator.
Comparison Table
| Option | Pros | Cons |
|---|---|---|
| Relative Locators | No need for stable IDs or CSS classes; works when the target element is consistently positioned near a reference element. | Sensitive to layout changes (responsive design, dynamic styling); can be slower because the driver must evaluate spatial relationships. |
| Traditional Locators (ID, name, CSS, XPath) | Fast and reliable when a unique attribute exists; immune to visual layout shifts. | Requires a unique attribute; may lead to brittle, long XPath/CSS selectors when attributes are generated or absent. |
Trade‑offs
Relative locators reduce reliance on brittle selectors that break when the UI’s internal IDs change, but they introduce a new point of failure: any change in the visual layout (e.g., a CSS grid reflow, added padding, or a responsive breakpoint) can cause the locator to miss the intended element. Traditional locators remain the fastest and most stable choice when a unique attribute is available, but they force you to write more complex selectors when such attributes are missing.
Implementation Example (Java)
The following snippet illustrates how to locate an <input> that sits directly below a label with a stable id. Replace the placeholder values with those that match your application.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.locator.RelativeLocator;
public class RelativeLocatorDemo {
public static void main(String[] args) {
// Set up the driver – ensure chromedriver matches your Chrome version
WebDriver driver = new ChromeDriver();
try {
driver.get("http://example.com/login"); // replace with your test page
// Step 1: locate a stable reference element
WebElement label = driver.findElement(By.id("username-label"));
// Step 2: use a relative locator to find the input below the label
WebElement input = driver.findElement(
RelativeLocator.with(By.tagName("input"))
.below(label)
);
// Step 3: interact with the element
input.sendKeys("testuser");
} finally {
driver.quit();
}
}
}
Explanation:
RelativeLocator.with(By.tagName("input"))creates a locator that will match any<input>element..below(label)constrains the match to elements positioned visually below the previously located label.- The code assumes the label is uniquely identifiable by
id="username-label"; if your reference element uses another attribute, adjust theByselector accordingly.
Verification Steps
- Create a minimal HTML page that contains a label with
id="username-label"and an<input>element directly beneath it (no other intervening elements). - Run the Java program against that page using a local WebDriver (e.g., ChromeDriver).
- Check that the
sendKeyscall completes without throwingNoSuchElementException. - Open the browser’s developer tools, inspect the DOM, and confirm that the element returned by the relative locator matches the intended
<input>(you can loginput.getTagName()andinput.getLocation()to verify). - Optionally, assert that the input’s
ycoordinate is greater than the label’sycoordinate to validate the “below” relationship.
Limitations and Practical Checks
Even when the reference element is stable, relative locators can become flaky if:
- The UI uses responsive design that moves elements at different breakpoints.
- Dynamic styling (e.g., animations, hover effects) temporarily changes the render order.
- The reference element itself is not uniquely locatable (e.g., it shares a class with many siblings).
To mitigate these risks:
- Choose a reference element that is both uniquely identifiable and unlikely to move (e.g., a header or a form field with a static
id). - Run your test at a fixed browser window size or disable responsive breakpoints during test execution.
- After locating the target element, verify additional attributes (such as
nameorplaceholder) to ensure you have the correct element.
If any of these checks fail, fall back to a traditional locator (ID, name, CSS, or XPath) for that particular test step.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.