We use cookies to analyze site traffic and show personalized ads. You can accept all cookies or decline personalized advertising.
Learn more in our Privacy Policy.
Combine multiple spatial conditions to target elements precisely.
Chaining relative locators lets you describe where an element is on the page using more than one spatial rule at the same time.
Instead of "find the button below the password field", you can say:
"Find the button that is below the password field and to the right of the Remember Me checkbox."
This makes your locator much more precise and reduces the chance of selecting the wrong element.
What it does
In Selenium, you use RelativeLocator.with(By...) to start a relative locator. You can then chain multiple spatial methods.
All chained conditions must be true for the element to match.
A single direction (only below(...)) might still match several elements.
By combining directions like below(emailLabel).toRightOf(rememberCheckbox), you reduce ambiguity and get exactly the element you want.
id="password" input). Use relative locators for elements whose attributes might change. Don't over-chain — use just enough spatial rules to be unique and readable.Practice chaining relative locators to find buttons below the password field and relative to the checkbox.
1234567891011121314151617181920212223242526272829WebElement passwordInput = driver.findElement(By.id("password"));
WebElement rememberCheckbox = driver.findElement(By.id("remember-me"));
WebElement loginButtonRef = driver.findElement(By.id("btn-login"));
WebElement loginButton = driver.findElement(
with(By.tagName("button"))
.below(passwordInput)
.toRightOf(rememberCheckbox)
);
loginButton.click();
WebElement cancelButton = driver.findElement(
with(By.tagName("button"))
.below(passwordInput)
.toLeftOf(loginButtonRef)
);
cancelButton.click();
List<WebElement> buttonsNearRemember = driver.findElements(
with(By.tagName("button"))
.below(passwordInput)
.near(rememberCheckbox)
);
for (WebElement btn : buttonsNearRemember) {
System.out.println(btn.getText());
}
driver.quit();id="password".id.<button>. Filters to buttons below the password input. Further filters to buttons to the right of the Remember Me checkbox. The final result should be the Login button.<button> elements below the password input. Now filters to buttons to the left of the known Login button. The final result should be the Cancel button.findElements to return all buttons that are below the password input and visually near the Remember Me checkbox.If .below(passwordInput) is too broad, add .toRightOf(rememberCheckbox) or .toLeftOf(loginButtonRef) to uniquely identify the element.
Choose elements with reliable attributes (id, name) as anchors (like the password input or checkbox), then use relative locators to find nearby buttons or inputs.
A chain like with(By.tagName("button")).below(passwordInput).toRightOf(rememberCheckbox) is more understandable and maintainable than a very complex XPath, and often more robust when the UI changes slightly.