Overview –

Selenium 4 Alpha 3 changes are pushed in Maven. Ther are so many changes it is bringing but we will be having a look at one of the important changes which are the Relative locator which earlier was friendly locator.

What are the Relative locators?

As the name suggests, its relative position of webelement.

Selenium 4 alpha-3 is released with much-awaited Relative locators which work very much like xpath axes. These new locator methods will find elements based on their relative location to other webobjects, Using relative locator an element can be located easily by

  • above(): target element which is above specified element
  • below():  target element which appears below the specified element
  • toLeftOf():  target element which appears to the left of a specified element
  • toRightOf():  target element which appears to the right of a specified element
  • near():  target element which is almost 50 pixels away from the specified element. 

How to use it, let’s take an example.

Therefore Relative work similarly Xpath Axes concept and identifies relative webobject.

Lets write a small code for it-

Configure Maven PoM file

First of all add selenium 4-aloha version, by adding a dependency in maven, as below-

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">


Example of Relative locator in below example-

In below code, Code is launching Orange HRM login page and with the help of Relative locator filling values in Username and Password-

In below code-

1- Admin value is inserted using “LOGIn Portal” Caption highlighted in Red circle

2- Password sendkeys are executed using Admin text box property

package Test;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;

import static org.openqa.selenium.support.locators.RelativeLocator.withTagName;

public class Dummyrun {

    WebDriver driver;

    public void setUp() throws InterruptedException {

        driver = new ChromeDriver();

    public void relativeLocatorTest() throws InterruptedException {

		  WebElement ele_B = driver.findElement(By.cssSelector("div[id='logInPanelHeading']"));
		  WebElement ele_A =driver.findElement(By.cssSelector("div[id='divUsername']"));
         WebElement UserName =  driver.findElement(withTagName("input")

         WebElement Password =  driver.findElement(withTagName("input")

    public void tearDown() {
        if(driver != null) {

Other changes in V4- alpha-3

Apart from Relative locators, please have a look at what other changes in alpha3 it brings-

Refer below link for detail change logs-


Change log in v4.0.0-alpha-3

  • Add “relative” locators. The entry point is through the- RelativeLocator.
    Usage is like driver.findElements(withTagName("p").above(lowest));
  • Add chromedriver cast APIs to remote server (#7282)
  • By is now serializable over JSON.
  • Add ApplicationCache, Fetch, Network, Performance, Profiler,
    ResourceTiming, Security and Target CDP domains.
  • Fixing Safari initialization code to be able to use Safari Technology
  • Ensure that the protocol converter handles the new session responses
  • Expose devtools APIs from chromium derived drivers.
  • Expose presence of devtools support on a role-based interface
  • Move to new Grid, deleting the old standalone server and grid implementation.
  • Switch to using HttpHandler where possible. This will impact projects that
    are extending Selenium Grid.
  • Respect “webdriver.firefox.logfile” system property in legacy Firefox driver.
    Fixes #6649
  • Back out OpenCensus support: OpenTracing and OpenCensus are merging, so
    settle on one for now.
  • Only allow CORS when using a —allow-cors flag in the Grid server
  • If you’re using the Java Platform Module System, all modules
    associated with the project are generated as “open” modules. This will change in a future release.
  • The version of Jetty being used is unshadowed.


We learned that Relative locator worked very similar to what Xpath AXES use to do.

Predecessor, chile, parent, ancestor etc . Instead of writing complex Axes we can use Relative locator to achieve web object identification where direct Xpath is difficult to find.

Hope this post helped you. Happy Testing !!!