Relative locators in Selenium allow you to pinpoint web elements based on where they appear in relation to other elements. Instead of relying only on IDs or classes, you can now describe elements just like a human would: "this field is below the username," or "that button is to the right of the product name."
These locators are especially useful in dynamic layouts where attributes change frequently. They make your test scripts more readable, easier to maintain, and more resilient across UI updates. Whether you’re working on form validations or product pages, relative locators can simplify your automation workflow.
In this article, we’ll explore:
Let’s get started!
Relative locators in Selenium help you find page elements by describing their position relative to other elements.
Instead of relying on attributes like ID or class, you can say an element is “above” another, “below” it, “toLeftOf”, “toRightOf”, or even “near” it. It mirrors the way you might explain things in real life.
Imagine looking for a new cafe in a busy neighborhood. You might say “the cafe is right next to the bakery.” That simple description gets you there fast. In Selenium tests, relative locators work the same way. You tell the script to look for an element in relation to something familiar on the page.
These locators also make scripts easier to read and maintain. When UI structure shifts slightly, your test can still find the right element based on its position. It keeps your automation flow strong and easy to follow for anyone on your team.
Selenium 4 brought in a useful feature: relative locators in Selenium are now a built-in part of the API. Before Selenium 4, testers often relied on complex XPath or custom scripts to work around missing element identifiers. Now it is much simpler and more intuitive to locate elements.
Here are the five core methods introduced:
These relative locators were not present in Selenium 3. In earlier versions you needed custom logic or complex XPath to handle spatial relationships. Selenium 4 makes these methods first-class, making your test code more human-readable and easier to maintain.
Relative locators in Selenium add clarity to your test scripts. They bring real advantages that simplify both automation and collaboration.
Relative locators in Selenium offer clarity and flexibility. They let you describe elements by position, which works well in many real-world scenarios. Still, knowing their limits helps you write more robust tests.
Relative locators work best when they complement, rather than replace, traditional strategies like ID or CSS. Testing a locator before adding it to your suite ensures it targets the intended element with confidence.
Relative locators in Selenium prove their value in real-world workflows. They help you target elements based on their logical position. That keeps tests simple yet effective in constantly changing UIs.
These scenarios show how relative locators in Selenium reduce dependency on brittle attributes. Your automation flow becomes clear and more resilient. That leads to broader coverage with less maintenance effort.
Choosing the right locator improves both performance and maintainability. Relative locators in Selenium and traditional locators each bring unique advantages to automation. Here's how they compare:
| Aspect | Traditional Locators | Relative Locators |
|---|---|---|
| Stability | Most stable when using unique IDs | Reliable when IDs or classes vary |
| Readability | Less readable in long XPath expressions | Easier to read and write |
| Maintainability | More updates needed in dynamic UIs | Less affected by layout shifts |
| Flexibility | Best for complex queries with XPath or CSS | Ideal for spatial relationships between elements |
Relative locators in Selenium simplify tests when unique attributes aren't available. Traditional locators provide strong performance when working with fixed identifiers. For the best outcome, use stable locators like ID first. Then combine or switch to relative locators when your UI changes frequently or lacks consistent identifiers.
Working with relative locators in Selenium follows a clear pattern. First, identify the reference element. Then apply the spatial method you need. Finally, interact with the element you're targeting. Here's a practical demonstration using both Java and Python.
Step 1: Identify the reference element, such as a label or button that stays stable in the layout.
Step 2: Use a relative method like below() or toRightOf() to locate the target.
Step 3: Interact with the element as you would with any standard locator.
Here’s how this works in Java:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import static org.openqa.selenium.support.locators.RelativeLocator.with;
WebDriver driver = new ChromeDriver();
driver.get("https://katalon.com");
WebElement referenceElement = driver.findElement(By.id("username"));
WebElement passwordField = driver.findElement(with(By.tagName("input")).below(referenceElement));
passwordField.sendKeys("myPassword");
And here’s the same logic using Python:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.relative_locator import locate_with
driver = webdriver.Chrome()
driver.get("https://katalon.com")
reference_element = driver.find_element(By.ID, "username")
password_field = driver.find_element(locate_with(By.TAG_NAME, "input").below(reference_element))
password_field.send_keys("myPassword")
Always test your relative locators to ensure they point to the intended elements. This practice guarantees your test logic works exactly as expected across layouts and updates.
Chainable relative locators in Selenium let you target elements with precision. You can combine spatial rules like “to the right of” and “below” to narrow down your selection. This makes your locator logic more accurate and less prone to ambiguity.
Use chaining when a single spatial descriptor may match multiple elements. It helps improve test accuracy by refining the search based on two spatial references.
Here is an example in Java illustrating chaining of relative locators:
import static org.openqa.selenium.support.locators.RelativeLocator.with;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
WebDriver driver = new ChromeDriver();
driver.get("https://katalon.com");
WebElement productName = driver.findElement(By.cssSelector(".product-title"));
WebElement priceTag = driver.findElement(with(By.tagName("span"))
.toRightOf(productName)
.below(driver.findElement(By.cssSelector(".product-image"))));
Here is the equivalent example in Python:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.relative_locator import locate_with
driver = webdriver.Chrome()
driver.get("https://katalon.com")
product_name = driver.find_element(By.CSS_SELECTOR, ".product-title")
price_tag = driver.find_element(
locate_with(By.TAG_NAME, "span")
.to_right_of(product_name)
.below(driver.find_element(By.CSS_SELECTOR, ".product-image"))
)
Chaining multiple spatial rules helps your locator stay precise as the UI evolves. Use chaining wisely and avoid overly complex combinations. That keeps your test logic clear and maintainable.
Relative locators in Selenium make tests more intuitive, but real-world layouts can still challenge them. When issues arise, a clear debugging approach helps you stay confident in your automation logic.
Effective debugging includes confirming that your relative locator points at the intended element. Try logging its attributes or visually checking the page. This helps avoid false positives and keeps your test suite reliable.
Relative locators in Selenium unlock powerful ways to reference elements visually. Following a few focused habits ensures they work consistently across your tests.
Good locator design always contributes to robust and readable test scripts. With these habits, relative locators in Selenium become an asset in every test scenario.
Katalon is built on top of Selenium and designed to make automation faster, easier, and more scalable. It takes the power of relative locators in Selenium and wraps it in a platform that is visual, intelligent, and team-friendly.
In short, Katalon makes Selenium more productive. It offers the power of automation in a scalable, team-friendly package that grows with your projects.
📝 Want to explore what Katalon can do for your team? Request a demo to see how it helps teams automate faster with less effort.