What are relative locators in Selenium 4.0?


The relative or friendly locators in Selenium 4.0 are available with the tagname attribute of the element.

  • above() - Webelement located above with respect to the specified element.

    Syntax −

    driver.findElement(withTagName(“<<tagnamevalue>>”).above(element));

  • below() - Webelement located below with respect to the specified element.

    Syntax −

    driver.findElement(withTagName(“<<tagnamevalue>>”).below(element));

  • toLeftof() - Webelement located to the left of the specified element.

    Syntax −

    driver.findElement(withTagName(“<<tagnamevalue>>”).toLeftOf(element));

  • toRightOf() - Webelement located to the right of the specified element.

    Syntax −

    driver.findElement(withTagName(“<<tagnamevalue>>”).toRightOf(element));

Code Implementation with relative Locators.

Example

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import static org.openqa.selenium.support.locators.RelativeLocator
.withTagName;
public class RelLocator {
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      String url = "https://www.tutorialspoint.com/about/about_careers.htm";
      driver.get(url);
      // maximizing browser with maximize()
      river.manage().window().maximize();
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      WebElement head_label = driver.findElement(By.cssSelector("li[class='heading']"));
      // getting the link text just below head_label web element
      String txt = driver.findElement(withTagName("a").below(head_label))
      .getText();
      System.out.println("The text below heading is " + txt);
      WebElement write =
      driver.findElement(By.xpath("//a[text()='Write for us']"));
      // getting the heading just above Write for us web link
      String txtabove = driver.findElement(withTagName("li").above(write))
      .getText();
      System.out.println("The text above link is " + txtabove);
      WebElement searchinp =
      driver.findElement(By.xpath("//input[@name='search']"));
      // getting the search button to the right of edit box searchinp.
      driver.findElement(withTagName("button").toRightOf(searchinp))
      .click();
      WebElement prntlnk =
      driver.findElement(By.xpath("//a[@class=' hide-on-mobile']"));
      // getting the previous page link to the left of prntlnk.
      String prevlink =
      driver.findElement(withTagName("a").toLeftOf(prntlnk))
      .getText();
      System.out.println("The text left of link is " + prevlink);
      driver.close();
   }
}

Updated on: 10-Jun-2020

435 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements