ByChained, ByAll, and Relative Locators Explained
Selenium 4 introduced powerful new ways to locate web elements that go far beyond traditional XPath. If you’re still relying only on long, brittle XPath expressions, you’re missing out on cleaner, more maintainable code. In this post, we’ll break down Advanced XPath in Selenium 4 — ByChained, ByAll, and Relative Locators — and show you how to use them with real Java examples.
Why Advanced XPath Alone Isn’t Enough in Selenium 4
Traditional XPath locators work well, but they can become long, fragile, and hard to read as your test suite grows. Selenium 4 solves this problem by giving testers built-in Java classes that combine or replace complex XPath logic with simple, readable code. This means fewer broken locators when the DOM structure changes, and test scripts that are easier for your whole team to maintain.
Let’s look at each strategy in detail.
1. ByChained: Combining Locators in Sequence
The ByChained class lets you chain two or more By locators together, similar to writing a nested XPath expression. Instead of writing something like //*[@id='login-form']//*[@id='username-field'], you can combine two separate locators directly in Java.
// The ByChained class enables you to chain two By locators togetherBy example = new ByChained(By.id("login-form"), By.id("username-field"));WebElement username_input = driver.findElement(example);
This searches for an element with the ID username-field that exists within an element with the ID login-form. It’s a clean alternative to writing nested XPath, and it keeps your locator logic readable.
2. ByAll: Matching Any of Multiple Locators
While ByChained requires elements to satisfy all locators in sequence, ByAll takes a different approach. It finds elements that match either of the given locators — useful when an element’s attributes might vary across environments or page versions.
/* The ByAll class enables you to utilize two By locators at once, finding elements that match either of your By locators */ By example1 = new ByAll(By.id("password-field"), By.id("username-field"));List<WebElement> login_inputs = driver.findElements(example1);
This is especially handy in test automation frameworks where the same test needs to run across slightly different page layouts or A/B test variants.
3. Relative Locators: Finding Elements Based on Position
Perhaps the most talked-about Selenium 4 feature is the Relative Locator (sometimes called “Friendly Locator”). Instead of relying purely on XPath axes like following-sibling or preceding, you can describe an element’s position relative to another element in plain, readable Java.
Locating Elements Above or Below
// Relative LocatorBy emailLocator = RelativeLocator.with(By.tagName("input")).above(By.id("password"));By passwordLocator = RelativeLocator.with(By.tagName("input")).below(By.id("email"));
Here, Selenium finds an input element positioned above or below a reference element on the page — no XPath axis syntax required.
Locating Elements to the Left or Right
By cancelLocator = RelativeLocator.with(By.tagName("button")).toLeftOf(By.id("submit"));By submitLocator = RelativeLocator.with(By.tagName("button")).toRightOf(By.id("cancel"));
This is ideal for form layouts where buttons or fields are positioned side by side, and traditional XPath would require verbose sibling-axis logic.
Locating Nearby Elements
By emailLocator1 = RelativeLocator.with(By.tagName("input")).near(By.id("lbl-email"));
The .near() method finds elements close to a reference point, which is great for form fields that sit beside their labels but don’t share a predictable DOM structure.
Chaining Relative Locators Together
You can also combine multiple relative locator conditions for more precise element targeting:
// Chaining Relative LocatorsBy submitLocator1 = RelativeLocator.with(By.tagName("button")) .below(By.id("email")) .toRightOf(By.id("cancel"));
This locates a button element that is both below the email field and to the right of the cancel button — a level of precision that would take a much more complex XPath expression to achieve.
Putting It All Together: A Practical Example
Here’s how these locator strategies might appear in a working TestNG test class using Selenium 4:
package com.seleniumtests.SeleniumTests;import java.util.List;import org.openqa.selenium.By;import org.openqa.selenium.WebElement;import org.openqa.selenium.support.locators.RelativeLocator;import org.openqa.selenium.support.pagefactory.ByAll;import org.openqa.selenium.support.pagefactory.ByChained;import org.testng.annotations.BeforeTest;import org.testng.annotations.Test;public class TestLocatorsAdvancedNotes extends BaseClass { static String browser = "chrome"; static String url = "https://demo.nopcommerce.com/"; @BeforeTest public static void preCondition() { init(browser, url); } @Test static void testLocatorsXPath() throws InterruptedException { By example = new ByChained(By.id("login-form"), By.id("username-field")); WebElement username_input = driver.findElement(example); By example1 = new ByAll(By.id("password-field"), By.id("username-field")); List<WebElement> login_inputs = driver.findElements(example1); By emailLocator = RelativeLocator.with(By.tagName("input")).above(By.id("password")); By passwordLocator = RelativeLocator.with(By.tagName("input")).below(By.id("email")); By cancelLocator = RelativeLocator.with(By.tagName("button")).toLeftOf(By.id("submit")); By submitLocator = RelativeLocator.with(By.tagName("button")).toRightOf(By.id("cancel")); By emailLocator1 = RelativeLocator.with(By.tagName("input")).near(By.id("lbl-email")); By submitLocator1 = RelativeLocator.with(By.tagName("button")) .below(By.id("email")) .toRightOf(By.id("cancel")); }}
Best Practices for Advanced Locators in Selenium 4
- Use
ByChainedwhen an element must sit inside a specific parent container. - Use
ByAllwhen you need flexibility across different page variants. - Use Relative Locators when position on the page is a more stable identifier than the DOM hierarchy.
- Combine relative locator methods sparingly — chaining too many conditions can make debugging harder.
- Always fall back to standard XPath when locator strategies above don’t provide enough precision.
Final Thoughts
Advanced XPath in Selenium 4 isn’t just about writing longer, smarter XPath strings — it’s about knowing when to reach for ByChained, ByAll, or Relative Locators instead. These tools make your test automation code more readable, more resilient to DOM changes, and easier for your team to maintain over time.
If you’re building a Selenium test suite, start replacing your most fragile XPath expressions with these Selenium 4 locator strategies today.
Related Links
✓ Selenium Locators Tutorial
✓ Selenium Waits Explained
✓ Page Object Model in Selenium
✓ Selenium Framework Design
✓ Selenium Grid Explained
✓ Jenkins Pipeline for Selenium Tests
✓ Complete Selenium Tutorial
Discover more from Rotebit
Subscribe to get the latest posts sent to your email.
