Selenium 4 Relative Locator (Friendly Locator)
Tags: selenium, testing, devops, test
Published: 2022-03-14 04:22:44

Relative Locators was the previous name for Friendly Locators. Relative Locators are used to locate a certain component or element in respect to the location of another component. There are five different techniques and two different arguments from which to choose. The Web Element component boundary or the by-finder border might be used. Below is a list of relative locators:

1) **above():** Finds a component or set of components that are positioned above a valid component.

2) **below():** Finds a component or set of components beneath a proper component.

3) **near():** Finds a component (or a set of components) that are close to a fixed component.

4) **toLeftOf():** Locates a component or components on the proper component's left side.

5) **toRightOf():** Finds a component (or several components) on the right side of a valid component.

Relative Locators connect the current categories of Selenium's 8 Locators: id, className, CSS selector, linkText, name, partial link text, tagName, and XPath. Finding out where to hunt for a component is one of the first responsibilities for an automation engineer.

### Import Relative Locator
There are two import declarations in Relative Locators. The first import declaration is static, whereas the second one is not. The syntax of our Relative Locator is affected by this discrepancy.

In one declaration, we can use static in the import statement, but not in the other. If we didn't use static in the import declaration, our code would look like this:

![test package](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/et72cnog22027sh6purh.png)
It's worth noting the import declaration org.openqa.selenium.support.locators. RelativeLocator; In the IntelliJ IDE, this package was automatically imported as a convenient solution. The package can be used to build a driver. findElement(RelativeLocator.withTagName(“”)). It's correct and will work properly. In any case, as illustrated below, we can start our sentence with TagName:
![import relative locator](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zn0lwirob9us341zrs3i.png)
If we don't change our import statement and remove RelativeLocator. before withTagName(""), we'll get an error that says "The technique withTagName(String) is undefined or not resolved for the type."

To figure out where the issue is originating from, we utilise static in the import statement before the bundle. Finish with a dot administrator (.) and (*) or withTagName(" "), as seen below:
![webdriver test package](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9827wgy4wlukhjiotj5m.png)

### Use of Relative Locator
Relative locator techniques are overused in Selenium 4, and they can take relative WebElement or By locater as input. The code below illustrates how to use a relative locator:![code for relative locator](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b43ah9hupjrmopgnqxzq.png)
![relative locator code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tbsdelkziciosuzm3l84.png)
![relative locator code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2tt36jprrxarxyrtt1dr.png)

- Xpath locates the Submit Button element.
- The tag name input is used by Next Script to discover elements.
- Above the submit button is the element input.
- The message is finally typed into the Message Box.
![demo relative locator](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tubuzguj8ri6z70k6svz.png)

### Find List of Web Element Using Relative Locator
We can use relative locators to find a list of web elements in the same manner that we can find a single element. We are able to locate all of the social icons in our example using relative locators:
![web element relative locator](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jzlj9taq9562pomlmkic.png)
![finding social icon](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6gwgncwoafs9kn1t5vk8.png)
Designers may now access local web components with fewer lines of code thanks to the introduction of relative locators in Selenium 4. The highlights may change in following editions due to the fact that this is an Alpha version. It's worth noting that in Selenium 4, techniques like above, below, toLeftOf, toRightOf, and close won't work with covering components.

Selenium 4 is a must-have for everyone who uses Selenium with Java for automated cross-program testing (Alpha). However, devices such as Selenium 4 Relative Locator (both open-source and commercial) have advantages. Selenium 4 comes with a slew of new capabilities (including updated documentation) that make the wait worthwhile.