Examples on Relative Locators

Note : you need to create a maven project and you need add Selenium 4.0.0-alpha-3 dependency.

Relative Locators using below option

Enter UserName and Password
Click in Sign-in link

The web element located below the password input box is the Sign-in link. As the relative location of password input box is already known, the below option is used to locate the Sign-in link.

 import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.locators.RelativeLocator; 
import io.github.bonigarcia.wdm.WebDriverManager;

public class relativeLocatorsExp {
     
      public static void main(String[] args) { 
         WebDriver driver;
         WebDriverManager.chromedriver().setup();
         driver=new ChromeDriver();        
         driver.get("http://newtours.demoaut.com/");
   driver.findElement(By.xpath("//input[@name='userName']")).sendKeys("hello");
 driver.findElement(By.xpath("//input[@name='password']")).sendKeys("hello");
      driver.findElement(RelativeLocator.withTagName("input").below(By.xpath("//input[@name='password']"))).click();
   }
}


Relative Locators using Above option
Sceanrio: 
Print displayed current date.


The web element located Above the Find A Flight is the Current Date. As the relative location of Find A Flight is already known, the Above option is used to locate the Current Date.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.locators.RelativeLocator; 
import io.github.bonigarcia.wdm.WebDriverManager;

public class relativeLocatorsExp {
     
      public static void main(String[] args) { 
         WebDriver driver;           
         WebDriverManager.chromedriver().setup();
         driver=new ChromeDriver();        
         driver.get("http://newtours.demoaut.com/");
         String currentDate=driver.findElement(RelativeLocator.withTagName("b").above(By.xpath("//img[@alt='Find a Flight']"))).getText();           
      System.out.println(currentDate);
      } 
}


Relative Locators using toRightOf option

Scenario
Enter the UserName base on username Label


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.locators.RelativeLocator;
import io.github.bonigarcia.wdm.WebDriverManager;

public class relativeLocatorsExp {
     
      public static void main(String[] args) { 
            WebDriver driver;           
            WebDriverManager.chromedriver().setup();
             driver=new ChromeDriver();                    
            driver.get("http://newtours.demoaut.com/");     
           
driver.findElement(RelativeLocator.withTagName("input").toRightOf(By.xpath("//font[contains(text(),'Name:')]"))).sendKeys("hello");
      }
}


Relative Locators using toLeftOf option

Scenario

Select the Female Gender  radio button


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.locators.RelativeLocator;
import org.openqa.selenium.support.ui.Select; 
import io.github.bonigarcia.wdm.WebDriverManager;

public class relativeLocatorsExp {
     
      public static void main(String[] args) { 
         WebDriver driver;           
         WebDriverManager.chromedriver().setup();
         driver=new ChromeDriver();        
         driver.get("https://www.facebook.com/");
         WebElement radioObj=driver.findElement(RelativeLocator.withTagName("input").below(By.xpath("//div[text()='Gender']")).toLeftOf(driver.findElement(By.xpath("//label[text()='Female']"))));
        radioObj.click();
      }}

Comments