All IT Courses 50% Off
Selenium Tutorials

What Are Selenium Relative Locators And How To Use Them

Finding web elements is the automation tester’s primary responsibility when creating test scripts for web applications. The current Selenium locators (id, name, className, linkText, partialLinkText, tagName, cssSelector, xpath) are already known to us. This page contains information on selenium locators. It should be mentioned that JavaScriptExecutor is used to circumvent the situation where elements cannot be found using these locators.

We can locate web items on the DOM with the aid of Selenium 3’s eight locators, which are ID, Name, class, XPath, CssSelectors, LinkText, PartialLinkText, and TagName. A new feature in Selenium 4 is called relative locators, which were formerly known as friendly locators. Check out our Selenium course online to learn more.

They are easy to express architecturally where an element is in relation to a readily located element, but they are useful when it is difficult to establish a locator for the desired element. Simply put, relative locators let us find web elements based on where they are in relation to other web elements.

Selenium 4 locates neighbouring items by using the JavaScript method getBoundingClientRect() to ascertain the size and location of web elements on the page.

A new locator, Friendly locators, was added to the list of locators in Selenium 4.0 and subsequently renamed as Relative locators.

All IT Courses 50% Off

By taking into account other online elements like above, below, toLeftOf, toRightOf, and near, it aids in helping us locate the web elements based on their position.

Put another way, relative locators let us find web elements based on where they are in relation to other web elements.

What Are Selenium Relative Locators And How To Use Them

In Selenium 4, five additional locators have been added:


It is to locate a web element just above the specified element


It is to locate a web element just below the specified element


It is to locate a web element present on the left of a specified element


It is to locate a web element present on the right of a specified element


The task involves locating a web element around 50 pixels away from a given element. One can pass the distance to an overloaded method as an argument.

Note: A new method called “withTagName()” that returns a RelativeLocator instance has been added. This function “withTagName()” is supported by the relative locators above.

Chaining Relative locators

Occasionally, just a single relative locator to locate an element is insufficient. A technique known as chaining Relative Locators is supported by Selenium 4 to locate a fixed web element using several relative locators. Depending on where the sought element is, we can mix many relative locators.

Assume that we have to click the “Get a Demo” button, which is located to the right of the “Get started free” link and beneath the “Text Section” section. Combining the below() and toRightOf() methods will allow you to land on the “Get a demo” button.

What Are Selenium Relative Locators And How To Use Them

Advantages of Relative Locators in Selenium 

  • Because they locate the required web element using common language, such as above, toRightOf, or next to any web element, relative locators are straightforward to construct and apply. As a result, the script is easier to read and comprehend.
  • It does this by removing the need for intricate combinations of XPath or CSS selectors, which minimises code complexity.
  • Time is saved since testers don’t have to spend as much time using XPath and CSS Selectors to create intricate locators.
  • Elements without unique attribute values, elements with dynamic values, and unstable applications can all benefit from relative locators.

Limitations of Relative Locators in Selenium

  • It is important to exercise extra caution when it comes to the size of the window that tests are running on when using relative locators. The window elements would not be in the same place if they were resized, and they might overlap, making it possible that the requested element would not be found. Relative locator techniques, such as above, below, toLeftOf, toRightOf, and near, do not function with overlapping elements in these situations.
  • There could be a small performance hit when using relative locators because the WebDriver has to do extra work to locate elements based on how they relate to other elements.
  • Relative locators rely on neighbouring web items being visible. Relative locators may lead to improper behaviour or test failure if the nearby web items are not visible.
  • If there are major structural changes to the webpage, the relative locators may need to be updated accordingly.


The above(), below(), toLeftOf(), toRightOf(), and near() locators are additional layers of finding elements added by relative locators. Knowing the location of another element facilitates the process of locating elements. For more accurate results, it is always important to test the website on actual devices with actual users and at various screen resolutions. To learn more about Relative Locators, check out the Selenium training online.

Facebook Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles

Back to top button