Selenium 4 Relative Locators
Selenium 4 introduced Relative Locators (also called Friendly Locators) to locate web elements based on their position relative to another element.
Instead of locating an element using only its ID, XPath, or CSS selector, you can locate it based on its position on the webpage.
Types of Relative Locators
| Locator | Description |
|---|---|
above() |
Finds an element above another element |
below() |
Finds an element below another element |
near() |
Finds an element near another element |
toLeftOf() |
Finds an element to the left of another element |
toRightOf() |
Finds an element to the right of another element |
1. Maven Dependency
Use Selenium 4 in your Maven project.
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.27.0</version>
</dependency>
2. Import Required Packages
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;
3. Example HTML
Consider the following HTML structure:
<label>Username</label>
<input id="username" type="text">
<label>Password</label>
<input id="password" type="password">
<button id="login">Login</button>
4. above() – Locate Element Above Another Element
Find the element located above the Login button.
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
WebElement login = driver.findElement(By.id("login"));
WebElement password = driver.findElement(
with(By.tagName("input")).above(login)
);
System.out.println(password.getAttribute("id"));
5. below() – Locate Element Below Another Element
Find the element located below the Username label.
WebElement usernameLabel =
driver.findElement(By.xpath("//label[text()='Username']"));
WebElement username = driver.findElement(
with(By.tagName("input")).below(usernameLabel)
);
System.out.println(username.getAttribute("id"));
6. toLeftOf() – Locate Element to the Left
Find an element positioned to the left of another element.
WebElement login = driver.findElement(By.id("login"));
WebElement leftElement = driver.findElement(
with(By.tagName("input")).toLeftOf(login)
);
System.out.println(leftElement.getAttribute("id"));
7. toRightOf() – Locate Element to the Right
WebElement username = driver.findElement(By.id("username"));
WebElement rightElement = driver.findElement(
with(By.tagName("input")).toRightOf(username)
);
System.out.println(rightElement.getAttribute("id"));
8. near() – Locate Nearby Elements
Find an element near another element.
By default, Selenium considers elements within approximately 50 pixels.
WebElement username = driver.findElement(By.id("username"));
WebElement nearbyElement = driver.findElement(
with(By.tagName("label")).near(username)
);
System.out.println(nearbyElement.getText());
You can also specify a custom distance:
WebElement nearbyElement = driver.findElement(
with(By.tagName("label")).near(username, 100)
);
9. Combining Multiple Relative Locators
You can combine multiple positional conditions.
WebElement login = driver.findElement(By.id("login"));
WebElement element = driver.findElement(
with(By.tagName("input"))
.above(login)
.toLeftOf(login)
);
System.out.println(element.getAttribute("id"));
10. Relative Locators with findElements()
You can retrieve multiple matching elements using findElements().
WebElement login = driver.findElement(By.id("login"));
List<WebElement> elements = driver.findElements(
with(By.tagName("input")).above(login)
);
for (WebElement element : elements) {
System.out.println(element.getAttribute("id"));
}
11. Real-Time Automation Example
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.List;
import static org.openqa.selenium.support.locators.RelativeLocator.with;
public class RelativeLocatorExample {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
try {
driver.get("https://example.com");
WebElement loginButton =
driver.findElement(By.id("login"));
// Find input above Login button
WebElement password = driver.findElement(
with(By.tagName("input")).above(loginButton)
);
System.out.println(
"Password field: " + password.getAttribute("id")
);
// Find elements above Login button
List<WebElement> elements = driver.findElements(
with(By.tagName("input")).above(loginButton)
);
System.out.println("Total elements: " + elements.size());
} finally {
driver.quit();
}
}
}
12. Relative Locators vs Traditional Locators
| Feature | Traditional Locators | Relative Locators |
|---|---|---|
| ID | Yes | Yes, as a base locator |
| Name | Yes | Yes, as a base locator |
| XPath | Yes | Yes, as a base locator |
| CSS Selector | Yes | Yes, as a base locator |
| Position-based locating | Limited | Yes |
| Selenium version | Selenium 3 and 4 | Selenium 4 |
| Element relationship | Usually explicit | Based on visual position |
Important Points
- Relative Locators were introduced in Selenium 4.
- They use element positions and browser geometry.
- They are useful when elements do not have unique IDs or attributes.
- They can become unreliable when page layouts change.
- Prefer stable ID, name, and CSS locators when available.

No comments:
Post a Comment