50% OFF Residential Proxies for 9 months — use code IPR50 at checkout

Get The Deal
Back to blog

Mastering XPath preceding-sibling: A Beginner’s Guide

Marijus Narbutas

Last updated -
How to

Key Takeaways

  • The preceding-sibling finds elements before the current node in the code.

  • Don’t confuse preceding-sibling with following-sibling to avoid mistakes.

  • Always test XPath expressions in tools or code, especially when handling dynamic elements.

Ready to get started?

Register now

XPath is a powerful tool for locating web and XML elements. It relies on different XPath axes to define how nodes relate to each other in a structured document.

Here, you’ll learn the basics of the preceding-sibling, compare them with their counterparts, and learn how to use them in real code with Selenium.

By the end of this article, you’ll know how to avoid common mistakes and find XPath axes with confidence.

Understanding the preceding-sibling Axis in XPath

When you use the preceding-sibling axis, you’re telling XPath to look at nodes that come before the current one in the same hierarchy. Simply put, it finds a sibling element in code that you’ve already passed through.

To make it even clearer, here’s a practical example that shows how this entire process works:

  • Let’s say you need to find the preceding-sibling in this HTML list:
<ul>
  <li>Item 1</li>
  <li id="current">Item 2</li>
  <li>Item 3</li>
</ul>

2. Currently, you’re at the 'Item 2'. To select 'Item 1' and locate the previous element, you need to use the preceding-sibling syntax (preceding‑sibling::tagname) and put it like this:

//li[@id='current']/preceding‑sibling::li

This example demonstrates a basic XPath expression using the preceding-sibling axis to locate a particular element.

preceding-sibling vs following-sibling

Axis Syntax Use case Navigation
preceding-sibling preceding‑sibling::tagname Find older siblings in code order Backward
following-sibling following‑sibling::tagname Find younger siblings in code order Forward
  • Use preceding-sibling when you need nodes before the current one.
  • Use following-sibling when you need nodes after the current one.
  • Useful when traversing elements in lists, tables, or forms.

Both axes are part of XPath, which define the relationship between nodes. The preceding axis and following-sibling both belong to the same family.

XPath Examples with Selenium

Here’s how you use it in Selenium to interact with input elements and labels in a form:

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

driver = webdriver.Chrome()
driver.get("https://example.com/form")

label_elem = driver.find_element(By.XPATH, "//input[@name='email']/preceding-sibling::label")
print(label_elem.text)

Step-by-Step Breakdown

Here’s what’s happening in the code snippet above:

  • Launching WebDriver. You create a Chrome browser instance using Selenium.
  • Opening the target page. The get() method loads the form page.
  • Finding the label. The XPath expression starts at the input with name='email'. It uses the preceding-sibling axis to move one step back to the <label> element.

This works well when preceding-sibling elements follow a consistent structure in your XML document or HTML page.

Relative XPath Example

//input[@name='email']/preceding-sibling::label

This is specific and looks directly before the email input for a <label>. It’s useful when the label is a preceding element.

Dynamic XPath Example

//input[contains(@id, 'user')]/preceding-sibling::label

This XPath expression handles dynamic nodes whose IDs change or vary slightly. contains(@id, 'user') matches anything with 'user' in its ID. The preceding-sibling axis works the same way, but now adapts to changing content.

When XPath uses these approaches, you’re locating web elements more reliably, especially when working with large or shifting pages.

Both approaches are great examples of how XPath axes help in traversing elements to find a particular element, no matter how deep it may be buried.

Common Errors With preceding-sibling

  • Not understanding DOM order. Preceding in code means earlier in HTML, not necessarily above on the page.
  • Confusing tag names or node types. Be sure your elements match the correct names.
  • Using incorrect axes for the desired node. Utilizing preceding-sibling when you should be using following-sibling.

Mistakes happen when navigating XML documents or HTML. If you try to use the preceding axis on the first child, it won’t work. Similarly, XPath axes like the preceding axis (preceding::) will return any prior nodes, not just siblings.

Conclusion

Now you know how to define preceding-sibling axis and write an XPath expression to locate an element’s siblings in an XML document or HTML. You also understand how preceding-sibling and following-sibling differ, and how different XPath axes are used depending on the direction and relationship you're navigating.

While you may not be able to build complex XPath expressions with these tips, you will surely be able to write a code that traverses your XML document or HTML more effectively.

For more advanced examples and web scraping tools like Puppeteer, check out our article on Puppeteer Sharp and XPath .

FAQ

How to get immediate preceding-sibling?

If you need the immediate previous sibling regardless of tag name, use //node/preceding-sibling::[1].

What does double colon mean in XPath?

The :: separates the axis name from the node test, as in preceding‑sibling::div.

How to select multiple siblings?

Use without indexing: preceding‑sibling::li. This returns all older elements.

How does XPath differ from CSS selectors?

XPath supports axes, functions, and works directly on XML documents. CSS lacks sibling axes. You can learn more about XPath and CSS selectors in our XPath vs CSS selectors guide.

What is the difference between parent and ancestor in XPath?

parent:: finds the direct parent, and ancestor:: can move up multiple levels, not just the direct one.

How to traverse forward or backward in XPath?

Use following-sibling:: or following:: for forward, and preceding-sibling:: or preceding:: for backward. For broader movement, choose sibling vs general axes.

Create Account

Author

Marijus Narbutas

Senior Software Engineer

With more than seven years of experience, Marijus has contributed to developing systems in various industries, including healthcare, finance, and logistics. As a backend programmer who specializes in PHP and MySQL, Marijus develops and maintains server-side applications and databases, ensuring our website works smoothly and securely, providing a seamless experience for our clients. In his free time, he enjoys gaming on his PS5 and stays active with sports like tricking, running, and weight lifting.

Learn More About Marijus Narbutas
Share on

Related articles