Selenium 4 Relative Locators
Selenium 4 Relative Locators
Selenium 4 introduces an exciting feature called Relative Locators, earlier also known as “Friendly Locators”. These locators help in finding web elements based on their position relative to other elements. These locators make it easier to select elements that are nearby or positioned to other elements on the webpage. This is particularly useful in cases where traditional locators like ID, class name, or XPath might not be ideal. With Relative Locators, you can write more flexible and reliable automation scripts.
What Are Relative Locators?
Relative Locators in Selenium 4 are methods that allow you to find elements based on their position relative to other nearby elements. Instead of searching for elements based on their exact attributes like ID, class, or XPath, you can now find an element by specifying its position to another element.
New Methods in Selenium 4
Selenium 4 provides five new methods to locate elements relatively. These methods are:
Method | Description |
---|---|
above() | This method finds an element that is located directly above another element. The element should be searched above the specified element. When we pass a Locator/ WebElement, this method searches for the position above the specified element. |
below() | Finds an element that is located directly below another element. The element should be searched below the specified element. When we pass a Locator/ WebElement, this method searches for the position below the specified element. |
toLeftOf() | Finds an element that is located to the left of another element. The element should be searched to the left of the specified element. When we pass a Locator/ WebElement, this method searches for the position to the left of the specified element. |
toRightOf() | Finds an element that is located to the right of another element. The element should be searched to the right of the specified element. When we pass a Locator/ WebElement, this method searches for the position to the right of the specified element. |
near() | Finds an element that is located near another element, within a specified range. The element should be at most 50 pixels far away from the specified element. |
How to Use Relative Locators
To use Relative Locators in Selenium 4, you’ll need to create a WebDriver instance and then use these methods to identify elements to others. Below is an example of how you can use each method:
WebElement element = driver.findElement(RelativeLocator.with(By.tagName("input"))
.below(By.id("submitButton")));
In this example, the code is looking for an <input>
element that is located directly below the element with the ID “submitButton”.
Advantages of Using Relative Locators
Relative Locators provide several benefits in web automation:
- Better Readability: It makes the code easier to understand because the position is specified in simple terms like “above” or “below”.
- Increased Flexibility: They make your scripts more flexible and less dependent on the exact position or attributes of the elements.
- Resilient to UI Changes: If the UI layout changes slightly, the test might still pass, as long as the relative positioning remains the same.
Selenium 4 Relative Locators are a powerful new feature that simplifies web automation by allowing you to locate elements based on their position relative to others. By using methods like ‘above’, ‘below’, ‘toLeftOf’, ‘toRightOf’, and ‘near’, you can write more flexible and maintainable test scripts. Whether you’re automating simple forms or complex dynamic websites, these relative locators can be a great tool in your Selenium automation toolkit.