Get All Sibling Elements
To get all the sibling elements of a given web element using Selenium for Java,
- Find the following elements on the given web element by XPath with the XPath expression as
"following-sibling::*". - Find the preceding elements on the given web element by XPath with the XPath expression as
"preceding-sibling::*".
The following is a simple code snippet to get all the sibling elements of the web element element.
</>
Copy
element.findElements(By.xpath("following-sibling::*"))
element.findElements(By.xpath("preceding-sibling::*"))
These expressions returns List of WebElements. We may append these elements to get all the siblings in a single list.
Example
In the following program, we write Selenium Java code to visit Tutorialkart Home Page, and find all the sibling elements of the link element <a href="/?originalUrl=https%3A%2F%2Fwww.tutorialkart.com%2Fjava%2F">Java</a>. We shall print the outer HTML attribute of both the element and sibling elements to standard output.

Java Program
</>
Copy
import java.util.ArrayList;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class MyAppTest {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("https://www.tutorialkart.com/");
//child element
WebElement element = driver.findElement(By.xpath("//a[@href="/?originalUrl=https%3A%2F%2Fwww.tutorialkart.com%2F"/java/\"]"));
System.out.println("Element\n" + element.getAttribute("outerHTML"));
//get all sibling elements
List<WebElement> followingSiblings = element.findElements(By.xpath("following-sibling::*"));
List<WebElement> precedingSiblings = element.findElements(By.xpath("preceding-sibling::*"));
List<WebElement> siblings = new ArrayList();
siblings.addAll(followingSiblings);
siblings.addAll(precedingSiblings);
System.out.println("\nAll Siblings");
for(int i = 0; i < siblings.size(); i++) {
System.out.println(siblings.get(i).getAttribute("outerHTML"));
}
driver.quit();
}
}
Output
Element
<a href="/?originalUrl=https%3A%2F%2Fwww.tutorialkart.com%2Fjava%2F">Java</a>
All Siblings
<a href="/?originalUrl=https%3A%2F%2Fwww.tutorialkart.com%2Fcpp%2F">C++</a>
<a href="/?originalUrl=https%3A%2F%2Fwww.tutorialkart.com%2Fc-sharp-tutorial%2F">C#</a>
<a href="/?originalUrl=https%3A%2F%2Fwww.tutorialkart.com%2Fr-tutorial%2F">R</a>
<a href="/?originalUrl=https%3A%2F%2Fwww.tutorialkart.com%2Fpython%2F">Python</a>
<a href="/?originalUrl=https%3A%2F%2Fwww.tutorialkart.com%2Fswift-tutorial%2F">Swift</a>
<a href="/?originalUrl=https%3A%2F%2Fwww.tutorialkart.com%2Fkotlin-tutorial%2F">Kotlin</a>
<a href="/?originalUrl=https%3A%2F%2Fwww.tutorialkart.com%2Fkotlin-android-tutorial%2F"><span>Kotlin Android</span></a>
<a href="/?originalUrl=https%3A%2F%2Fwww.tutorialkart.com%2Fandroid-jetpack-compose%2F"><span>Android Jetpack Compose</span></a>
<a href="/?originalUrl=https%3A%2F%2Fwww.tutorialkart.com%2Fdart%2F">Dart</a>
<a href="/?originalUrl=https%3A%2F%2Fwww.tutorialkart.com%2Fflutter%2F">Flutter</a>
<a href="/?originalUrl=https%3A%2F%2Fwww.tutorialkart.com%2Fnodejs%2Fnodejs-tutorial%2F">NodeJS</a>
<a href="/?originalUrl=https%3A%2F%2Fwww.tutorialkart.com%2Ffasttext-tutorial%2F">FastText</a>
<a href="/?originalUrl=https%3A%2F%2Fwww.tutorialkart.com%2Fdocker%2Fdocker-tutorial%2F">Docker</a>
<a href="/?originalUrl=https%3A%2F%2Fwww.tutorialkart.com%2Fnginx%2Fnginx-tutorial%2F">NGINX</a>
<a href="/?originalUrl=https%3A%2F%2Fwww.tutorialkart.com%2Fkubernetes%2Fkubernetes-tutorial%2F">Kubernetes</a>
<a href="/?originalUrl=https%3A%2F%2Fwww.tutorialkart.com%2Fbash-shell-scripting%2Fbash-tutorial%2F"><span>Bash Scripting</span></a>
<a href="/?originalUrl=https%3A%2F%2Fwww.tutorialkart.com%2Fscipy-tutorial%2F">SciPy</a>
<a href="/?originalUrl=https%3A%2F%2Fwww.tutorialkart.com%2Fgit%2F">Git</a>
<a href="/?originalUrl=https%3A%2F%2Fwww.tutorialkart.com%2Fopencv%2Fpython%2F"><span>OpenCV Python</span></a>
<a href="/?originalUrl=https%3A%2F%2Fwww.tutorialkart.com%2Fjavafx%2F">JavaFX</a>
<a href="/?originalUrl=https%3A%2F%2Fwww.tutorialkart.com%2Fuml-tutorial%2F">UML</a>
<a href="/?originalUrl=https%3A%2F%2Fwww.tutorialkart.com%2Ftypescript%2F">TypeScript</a>
<a href="/?originalUrl=https%3A%2F%2Fwww.tutorialkart.com%2Fscala%2F">Scala</a>
<a href="/?originalUrl=https%3A%2F%2Fwww.tutorialkart.com%2Fjulia%2F">Julia</a>
<a href="/?originalUrl=https%3A%2F%2Fwww.tutorialkart.com%2Fnumpy-tutorial%2F">Numpy</a>
<a href="/?originalUrl=https%3A%2F%2Fwww.tutorialkart.com%2Fgolang-tutorial%2F">Golang</a>
Screenshot

