
During website test automation, testers often struggle to locate certain elements or fields that lack unique identifiers such as an ID or name.
The following-sibling XPath in Selenium helps them locate such elements by first identifying a stable, nearby element that shares the same parent and appears before the target in the page structure.
Testers can then select the target element based on its position relative to that stable sibling, side-navigating to reach it.
This method makes it possible to locate HTML elements that don't have a specific ID but are part of the same broader parent structure. This article explains how to use the following-sibling axis effectively in Selenium
Run Selenium Tests on Cloud
The following sibling XPath is a Selenium testing method in XPath that selects all the nodes having the same parent as the context (or current) node which appear after the context (current) node.
It lets you locate an element by navigating relative to another parent element you can already identify.
The actual "parent has ID, child doesn't" strategy that the following-sibling solves is a two-hop chain:
Given an HTML like
<div>
<label>Username</label>
<input type="text" name="user"/>
</div>If it <label> is easy to identify (stable text) but it <input> has no reliable ID or name, you can write:
//label[text()='Username']/following-sibling::inputThis says, "Find the label with the text 'Username,' then find the input that comes after it and shares the same parent."
You can also target a specific sibling position or tag:
//label[text()='Username']/following-sibling::input[1] The following sibling path allows Selenium to navigate the DOM tree systematically and locate elements based on their structural relationships rather than only attributes, ID, or text.
Read More: XPath in Selenium: A Complete
Axis form: always written as following-sibling::nodetest, attached after a step that identifies the "current" node.
node/following-sibling::tag_nameNode test options:
These position filters index the dropdown elements and count them in a document order from the current node, not from the start of the dropdown where the parent is.
node/following-sibling::li[1] → first sibling after node
node/following-sibling::li[last()] → last sibling after node //li[@class='active']/following-sibling::li[@data-type='fruit']//li[@id='current']/following-sibling::li[1]/spannode/following-sibling::input/@valueThis axis is most useful when an element doesn't have a reliable ID/class, but a nearby labeled element does. You anchor on the labeled element, then step sideways to the target.
1. Locating a form field next to its label
Here is the HTML script:
<div>
<label>Email Address</label>
<input type="text" name="email"/>
</div> Here is how you’ll declare the following sibling syntax:
//label[text()='Email Address']/following-sibling::input 2. Locating a button after a specific message
<div class="alert">Item added to cart</div>
<button>View Cart</button> [/prettycode]
The XPath syntax:
//div[text()='Item added to cart']/following-sibling::button3. Selecting table cells after a specific cell (same row)
<tr>
<td>Product</td>
<td id="target">Price</td>
<td>Stock</td>
<td>Action</td>
</tr>
The XPath syntax
//td[@id='target']/following-sibling::td[1] <!-- gets "Stock" -->4. Skipping unstable wrapper elements
When automation tools (like Selenium) can't find an element by ID because it's dynamically generated, but a stable sibling exists:
The XPath syntax:
//span[@class='stable-label']/following-sibling::div[1]5. Selecting multiple following siblings for iteration
The XPath syntax:
//h2[text()='Section A']/following-sibling::p Grabs all <p> tags after that heading; useful for scraping content until the next heading.
The following-sibling axis selects all siblings that appear after a reference element. It is useful when the target element consistently comes after a known element in the DOM.
For example, to select an input that comes immediately after a label:
//label[text()='Email']/following-sibling::inputThis selects the input field immediately after the label with the text "Email."
The preceding-sibling axis selects all siblings that appear before a reference element. It is helpful when the target element appears earlier in the DOM relative to a known element.
For example, to select a label that comes immediately before an input:
//input[@id='email']/preceding-sibling::labelThis selects the label that is positioned just before the input with ID email.
A few practices that make sibling-based expressions hold up over time:
Some recurring mistakes that cause sibling-based expressions to fail or misbehave:
XPath’s following-sibling method lets you select the related sub-menus or options that a user can click on from the main dropdown, even if they were kept obscure by the developers.
This streamlines the Selenium automation workflow for the website and helps you inspect, manage, and identify any common flaky tests related to these options faster.
XPath also ensures input fields, signup forms, buttons, and other controls are consistently identified even on dynamic pages.
BrowserStack provides a cloud-based platform, Bird Eats Bug, to identify and resolve bugs during your Selenium test automation for seamless, cross-browser testing. Validate your relationships in real time, identify rendering issues, and make seamless DOM interactions.
Get visual proof, steps to reproduce and technical logs with one click
Continue reading
Try Bird on your next bug - you’ll love it
“Game changer”
Julie, Head of QA
Try Bird later, from your desktop