Contents

    Guides

    Following-Sibling XPath in Selenium: Syntax, Examples, and Usage

    Published on

    September 29, 2025
    Following-Sibling XPath in Selenium: Syntax, Examples, and Usage

    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

    What Does the Following-Sibling XPath in Selenium Do?

    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:

    1. Anchor on the parent by its ID.
    2. Step down to a stable sibling inside that parent (with fixed text, a heading, or whatever has something to grab onto).
    3. Use the following sibling: from that stable sibling to reach the actual target, which has no identifiers of its own.

    Example of following-sibling XPath in Selenium

    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::input

    This 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

    Understanding Following-Sibling XPath

    Axis form: always written as following-sibling::nodetest, attached after a step that identifies the "current" node.

    node/following-sibling::tag_name

    Node test options:

    • following-sibling::* - matches any element type
    • following-sibling::li - matches only <li> siblings
    • following-sibling::text() - matches sibling text nodes

    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
    • Combining with predicates: you can filter the current node and the target siblings independently.
      //li[@class='active']/following-sibling::li[@data-type='fruit']
    • Chaining axes: you can continue navigating from the result.
    //li[@id='current']/following-sibling::li[1]/span
    • Attributes as node test: to select a following sibling's attribute rather than an element:
    node/following-sibling::input/@value

    Applying Following-Sibling XPath to Locate Elements

    This 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::button

    3. 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.

    Preceding-Sibling vs Following-Sibling XPath Axes

    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 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::label

    This selects the label that is positioned just before the input with ID email.

    Techniques for Writing Reliable Following-Sibling XPath

    A few practices that make sibling-based expressions hold up over time:

    • Stable anchoring: Anchor on the most stable nearby element rather than the target itself. If the target's ID or class keeps changing, fix the anchor and navigate sideways from there.
    • Text-based anchors: Prefer matching on stable text content over positional structure, and normalize whitespace so stray line breaks or extra spaces don't cause mismatches.
    • Attribute-based filtering: Avoid relying on exact sibling position. Filter siblings by an attribute or text value instead of a hardcoded index, since inserting or removing an element breaks position-based selectors.
    • Partial matching: Use partial text or attribute matching for dynamic class names, so you can target the stable portion of a class while ignoring auto-generated suffixes.
    • Tight scoping: Scope your anchor tightly (e.g., within a specific form or section) to avoid matching the wrong node when the anchor expression could match multiple elements.
    • Relative paths: Prefer relative paths over absolute ones, since the sibling axis is inherently relative, and pairing it with absolute paths reintroduces fragility.
    • Isolated testing: Test expressions in isolation using browser developer tools before wiring them into automation code to confirm the anchor is unique and the sibling relationship actually holds.

    Common Issues When Using Following-Sibling XPath

    Some recurring mistakes that cause sibling-based expressions to fail or misbehave:

    • Visual vs. DOM proximity: Assuming "sibling" means anything visually nearby. It only matches nodes sharing the same direct parent, so elements in separate containers won't match even if they look adjacent.
    • Whitespace text nodes: Forgetting that whitespace text nodes count. Selecting "any following sibling node" instead of "any following sibling element" can unintentionally include empty whitespace nodes.
    • Axis confusion: Confusing the sibling axis with the plain "following" axis, which selects everything after the current node in document order, not just direct siblings. This often pulls in unrelated, deeply nested elements.
    • Indexing misunderstanding: Misunderstanding indexing. A position filter refers to the element's position among the matched following siblings, not its overall position in the original list.
    • Case sensitivity: Overlooking case sensitivity. XPath is case-sensitive by default, which trips people up with inconsistently generated HTML.
    • Non-unique anchors: Anchor elements matching multiple nodes. If the anchor isn't unique, sibling results become unpredictable depending on which match the tool picks.
    • Late evaluation: Dynamic DOM changes after page load. In JavaScript-heavy apps, an expression correct against the final DOM can fail if evaluated before content finishes loading or reordering.
    • Wrong relationship type: Overreliance on the sibling axis when a simpler relationship exists. The target might actually be a child or descendant of a nearby element, so it's worth checking the real DOM structure rather than assuming based on visual layout.

    Conclusion

    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.

    Data-rich bug reports loved by everyone

    Get visual proof, steps to reproduce and technical logs with one click

    Make bug reporting 50% faster and 100% less painful

    Rating LogosStars
    4.6
    |
    Category leader

    Liked the article? Spread the word

    Put your knowledge to practice

    Try Bird on your next bug - you’ll love it

    “Game changer”

    Julie, Head of QA

    star-ratingstar-ratingstar-ratingstar-ratingstar-rating

    Overall rating: 4.7/5

    Try Bird later, from your desktop