Discover how a simple way to 'walk' through webpage elements can save hours of frustrating test failures!
Why XPath axes (parent, child, sibling) in Selenium Python? - Purpose & Use Cases
Imagine you have a big webpage with many nested elements. You want to find a button next to a specific label, but you only know the label's text. Manually clicking through the page or guessing element positions is like searching for a needle in a haystack.
Manually locating elements by trial and error is slow and frustrating. You might click the wrong button or miss the right one because the page structure changes often. This leads to flaky tests that fail randomly and waste your time.
XPath axes let you navigate the webpage structure smartly. You can move from a known element to its parent, children, or siblings easily. This means you can find related elements precisely, even if the page layout changes a bit.
button = driver.find_element(By.ID, 'submit') # Hardcoded, breaks if ID changes
button = driver.find_element(By.XPATH, "//label[text()='Name']/following-sibling::button") # Finds button next to label
With XPath axes, you can write tests that adapt to page changes and find elements based on their relationships, not just fixed IDs.
Testing a form where the submit button has no unique ID but is always next to a label. Using XPath axes, you find the button by locating the label first, then moving to its sibling button.
Manual element finding is slow and error-prone.
XPath axes let you navigate element relationships easily.
This makes tests more reliable and easier to maintain.
Practice
Solution
Step 1: Understand XPath axes
XPath axes describe relationships between nodes, such as parent, child, or siblings.Step 2: Identify the axis for parent node
Theparent::axis selects the direct parent of the current node.Final Answer:
parent:: -> Option BQuick Check:
Parent axis = parent:: [OK]
- Confusing parent:: with child::
- Using sibling axes to find parent
- Forgetting axes syntax with double colons
<li> elements of a <ul> element?Solution
Step 1: Understand child axis usage
Thechild::axis selects direct children of the current node.Step 2: Apply correct syntax for child
Usinglielements//ul/child::liselects alllichildren oful.Final Answer:
//ul/child::li -> Option DQuick Check:
Child axis syntax = child:: [OK]
- Using parent:: instead of child::
- Using sibling axes for children
- Omitting the axis and expecting child selection
<div> <h2>Title</h2> <p>Paragraph 1</p> <p>Paragraph 2</p> </div>
What does the XPath
//h2/following-sibling::p[1] select?Solution
Step 1: Understand following-sibling axis
Thefollowing-sibling::p[1]selects the firstpsibling after theh2node.Step 2: Apply to given HTML
Afterh2, the firstpis "Paragraph 1", so it selects that element.Final Answer:
The first <p> element inside the <div> -> Option CQuick Check:
following-sibling::p[1] = first p after h2 [OK]
- Selecting the second
pinstead of the first - Confusing preceding-sibling with following-sibling
- Thinking it selects children instead of siblings
div element://div/preceding-sibling::div[1]But it returns no elements. What is the most likely reason?
Solution
Step 1: Understand preceding-sibling axis
Thepreceding-sibling::div[1]selects the immediate previous siblingdivelement.Step 2: Reason why no elements returned
If no elements are returned, it means the currentdivhas no preceding siblingdiv.Final Answer:
The <div> has no preceding sibling <div> elements -> Option AQuick Check:
preceding-sibling returns nothing if no previous siblings [OK]
- Thinking axes can't be used with //
- Confusing parent:: with preceding-sibling::
- Assuming preceding-sibling selects children
span elements that are children of a div whose immediate parent is a section. Which XPath expression correctly uses axes to achieve this?Solution
Step 1: Understand the hierarchy
Thespanelements are children ofdiv, and thatdivis a child ofsection.Step 2: Build XPath with axes
//section/child::div/child::spanselectsspanchildren ofdivchildren ofsection.Step 3: Check other options
//div/parent::section/child::spanselectsspanchildren ofsection, which is incorrect.//section/parent::div/child::spanand D misuse axes or order.Final Answer:
//section/child::div/child::span -> Option AQuick Check:
Use child axes from section to div to span [OK]
- Using parent:: in wrong direction
- Selecting wrong node levels
- Mixing sibling axes instead of child axes
Ready to prove it?
Pass the test and this pattern moves unknown
