How to get all the sibling elements in Selenium Python?

Selenium Python – Get all the sibling elements

In this tutorial, you will learn how to get all the sibling elements of a given element, in Selenium Python.

To get all the sibling elements of a given element in Selenium Python, call the find_elements() method on the given element and pass By.XPATH for by parameter, and 'following-sibling::* | preceding-sibling::*' for the value parameter in the function call.

If myelement is the WebElement object for which we would like to find the sibling elements, the code snippet for find_elements() method is

myelement.find_elements(By.XPATH, "following-sibling::* | preceding-sibling::*")

The above method call returns a list of WebElement objects containing the sibling elements.


In this example, we shall consider loading the webpage at URL: The contents of this HTML file is given below. The web page contains a parent div with three children divs.

  <h1>Hello Family</h1>
  <div id="parent">
    <div id="child1">This is child 1.</div>
    <div id="child2">This is child 2.</div>
    <div id="child3">This is child 3.</div>
Selenium Python - Get parent element - Webpage

We shall take the child div div#child1 as our WebElement of interest, find all of its sibling elements, and print these elements to standard output.

Python Program

from selenium import webdriver
from import ChromeDriverManager
from import Service as ChromeService
from import By

# Setup chrome driver
driver = webdriver.Chrome(service=ChromeService(executable_path=ChromeDriverManager().install()))
driver.set_window_size(500, 400)

# Navigate to the url

# Get the div element you are interested in
mydiv = driver.find_element(By.ID, 'child1')

# Get siblings of mydiv
siblings = mydiv.find_elements(By.XPATH, "following-sibling::* | preceding-sibling::*")

# Itertae over the siblings
for sibling in siblings:
    print("\nSibling Element")

# Close the driver


Sibling Element
<div id="child2">This is child 2.</div>

Sibling Element
<div id="child3">This is child 3.</div>


  • for child2: child1 and child3 would be the sibling elements.
  • for child3: child1 and child2 would be the sibling elements.


In this Python Selenium tutorial, we have given instructions on how to find the sibling elements of a given web element, with the help of an example program.

Related Tutorials

Quiz on Selenium

Q1. What is Selenium Python used for?

Not answered

Q2. What is the method used to interact with web elements in Selenium Python?

Not answered

Q3. Which of the following is not a commonly used web element locator in Selenium Python?

Not answered

Q4. What is the method used to select a value from a drop-down list in Selenium Python?

Not answered

Q5. What is the method used to clear the text from an input field in Selenium Python?

Not answered
Code copied to clipboard successfully 👍