0
0
Selenium Pythontesting~5 mins

XPath axes (parent, child, sibling) in Selenium Python

Choose your learning style9 modes available
Introduction

XPath axes help you find elements related to a known element on a webpage. This makes locating elements easier and more precise.

When you want to find the parent element of a known child element.
When you need to locate child elements inside a specific parent element.
When you want to find sibling elements next to a known element.
When the element you want does not have a unique ID or class but is related to another element.
When you want to navigate the webpage structure logically instead of guessing element positions.
Syntax
Selenium Python
/parent::tagname
/child::tagname
/following-sibling::tagname
/preceding-sibling::tagname

Use parent:: to move up to the parent element.

Use child:: to find direct children of the current element.

Use following-sibling:: or preceding-sibling:: to find siblings after or before the current element.

Examples
Find the section element that is the parent of the div with id 'main'.
Selenium Python
//div[@id='main']/parent::section
Find all li elements that are direct children of the ul with class 'menu'.
Selenium Python
//ul[@class='menu']/child::li
Find the p elements that come right after the h2 with class 'title' as siblings.
Selenium Python
//h2[@class='title']/following-sibling::p
Find the input element that is a sibling following the label with text 'Username'.
Selenium Python
//label[text()='Username']/following-sibling::input
Sample Program

This script opens a simple HTML page in a headless browser. It uses XPath axes to find:

  • The parent section of a div with id 'main'.
  • All li children of a ul with class 'menu'.
  • The p element following an h2 with class 'title'.
  • The input element following a label with text 'Username'.

It prints the results to show the correct elements were found.

Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By

# Setup WebDriver (make sure chromedriver is in PATH)
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument('--headless')
driver = webdriver.Chrome(options=options)

# Open a simple test page
html = '''
<html>
  <body>
    <section>
      <div id='main'>Content here</div>
    </section>
    <ul class='menu'>
      <li>Home</li>
      <li>About</li>
      <li>Contact</li>
    </ul>
    <h2 class='title'>Welcome</h2>
    <p>This is a paragraph after title.</p>
    <label>Username</label>
    <input type='text' />
  </body>
</html>
'''

# Load HTML content
driver.get('data:text/html;charset=utf-8,' + html)

# Find parent of div#main
parent_section = driver.find_element(By.XPATH, "//div[@id='main']/parent::section")
print('Parent tag of div#main:', parent_section.tag_name)

# Find children li of ul.menu
children_li = driver.find_elements(By.XPATH, "//ul[@class='menu']/child::li")
print('Number of li children in ul.menu:', len(children_li))

# Find following sibling p of h2.title
following_p = driver.find_element(By.XPATH, "//h2[@class='title']/following-sibling::p")
print('Text of following sibling p:', following_p.text)

# Find following sibling input of label 'Username'
input_elem = driver.find_element(By.XPATH, "//label[text()='Username']/following-sibling::input")
print('Tag of following sibling input:', input_elem.tag_name)

driver.quit()
OutputSuccess
Important Notes

Always test your XPath expressions in browser DevTools first to make sure they find the right elements.

Using axes like parent:: or following-sibling:: helps when elements don't have unique IDs.

Axes navigation can make your tests more stable and easier to understand.

Summary

XPath axes let you find elements related by parent, child, or sibling relationships.

They help locate elements more precisely when IDs or classes are missing.

Using axes improves test reliability and clarity.