Testing complex web pages in Selenium can be challenging when elements lack unique identifiers. Traditional locators like IDs or classes often fail on dynamically generated elements or pages with shifting structures. Tests become fragile when elements move or their attributes change.
Following-sibling XPath addresses this problem by allowing testers to select elements based on their position relative to stable siblings. This method makes it possible to locate elements that would otherwise be difficult to target reliably.
This article explains how to use following-sibling XPath effectively in Selenium.
XPath axes describe the direction or relationship between a reference element (context node) and other elements in the DOM. They allow Selenium to navigate the DOM tree systematically and locate elements based on structural relationships rather than only attributes or text.
XPath axes can traverse in multiple directions:
The following-sibling axis in XPath selects all sibling elements that appear after a reference element in the DOM. It is particularly useful when elements do not have unique identifiers but maintain a consistent order relative to other elements. This axis allows precise targeting without relying solely on classes, IDs, or text values.
Following-sibling XPath uses the format:
reference_element/following-sibling::tag_name
For example, //label[text()='Username']/following-sibling::input[1] selects the first input element immediately after the label with text Username.
Following-sibling XPath can target elements based on text, partial matches, or attributes. Below are common approaches:
1. Selecting Elements by Exact Text
To locate an element that comes immediately after a label with a known text, use the following-sibling axis with the exact text of the reference element.
//label[text()='Password']/following-sibling::input
This selects the input field that directly follows the label containing Password.
2. Using contains(text()) for Partial Matches
When the reference label may have dynamic text or partial content, the contains() function can be used to match part of the text.
//label[contains(text(),'User')]/following-sibling::input
This selects the input field that comes immediately after a label containing the word User.
3. Targeting Elements via Attributes
If the elements do not have unique text but have consistent attributes, you can use the following-sibling axis with attribute conditions.
//div[@class='form-group']/following-sibling::div[@class='form-group']
This selects the next sibling div element with the class form-group.
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::input
This selects the input field immediately after the label with 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::label
This selects the label that is positioned just before the input with ID email.
Writing stable following-sibling XPath requires understanding both the DOM structure and the behavior of elements on the page. Unreliable XPath can break when elements move or page content changes.
Below are key techniques to make following-sibling XPath robust.
When possible, start from an element with a unique ID, text, or attribute. This ensures the reference node is stable.
//label[@id='username']/following-sibling::input
This selects the input field after a label with a unique ID username.
Indexes can specify which sibling to select but should be used carefully because DOM changes can shift positions.
//label[text()='Option']/following-sibling::input[2]
This selects the second input after the label with text Option.
Adding attribute conditions increases precision and reduces dependency on order alone.
//label[text()='Password']/following-sibling::input[@type='password']
This selects the input with type password after the label Password.
Avoid overly broad XPath that search large DOM sections. Limit the search scope by starting from a specific container or parent element.
//div[@class='login-form']//label[text()='Email']/following-sibling::input
This selects the input field after the label Email within a specific div container, reducing DOM traversal.
Following-sibling XPath are effective but can encounter specific challenges that may break tests. Understanding these issues helps create more reliable locators.
Testing XPath on real devices ensures that locators work reliably across different environments and screen sizes. Even correct XPath can fail if the page layout or rendering differs on actual devices.
To address these challenges, BrowserStack provides access to a wide range of real devices and browsers in the cloud. Testers can validate following-sibling XPath in authentic environments, detect layout or rendering issues, and ensure that scripts perform consistently without managing physical hardware.
Following-sibling XPath in Selenium locates an element that comes after a specific sibling. Use exact text to select a precise element, contains() to handle partial matches, and attributes to target elements when order changes. This ensures input fields, buttons, and other controls are consistently identified even on dynamic pages.
BrowserStack provides a cloud-based platform for running Selenium tests on a wide range of real devices and browsers. This allows testers to validate following-sibling XPath in real-world conditions, identify rendering issues, and ensure interactions like clicks and typing behave as expected without managing physical devices.
Run Selenium Tests on Cloud
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