What is following-sibling in Selenium?

We can use the concept of following-sibling in xpath for identifying elements in Selenium. It identifies the siblings of the context node. The siblings should be located at the equal level of the existing node and should have the same parent.

Let us see an example of an element with ul tag having more than one child with li tag. Then let us try to locate the fifth li element (Effective Resume Writing) from the first li element with class attribute sreading.



Here, we are locating the fifth child of ul tag, but we have provided li[4] since we are locating the fifth child from the first child element.


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class FollowingSiblingXpath{
   public static void main(String[] args) {
      WebDriver driver = new ChromeDriver();
      //implicit wait
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      //URL launch
      //identify fifth child from first child
      WebElement n = driver.findElement
      String s = n.getText();
      System.out.println("Text is: " + s);


Updated on: 06-Apr-2021

17K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started