---
title: "Mastering XPath preceding-sibling: A Beginner’s Guide"
description: "Master XPath with effective techniques focusing on the preceding-sibling. A guide tailored for complete beginners with easy-to-understand approaches."
url: "https://iproyal.com/blog/xpath-preceding-sibling/"
---

- [EN - English](https://iproyal.com/)
   
  [中文 - Chinese](https://iproyal.cn/)

[Book a Demo](https://iproyal.com/request-a-demo/) [Create Account](https://dashboard.iproyal.com/register/) [Create Account](https://dashboard.iproyal.com/register/) [Login](https://dashboard.iproyal.com/login/) [Login](https://dashboard.iproyal.com/login/)

[Book a Demo](https://iproyal.com/request-a-demo/)

[Back to blog](https://iproyal.com/blog/)

# Mastering XPath preceding-sibling: A Beginner’s Guide

[Marijus Narbutas](https://iproyal.com/blog/authors/marijus-narbutas/)

[Last updated - July 29, 2025 ‐ 4 min read](https://iproyal.com/blog/authors/marijus-narbutas/)

[Tutorials](https://iproyal.com/blog/category/tutorials/)

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

**XPath is a powerful tool for locating web and XML elements.&#x20;**&#x49;t 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,*&#x20;**&#x63;ompare 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&#x20;**&#x74;hat shows how this entire process works:

- Let’s say you need to **find the *preceding-sibling* in this HTML list:**

```html
<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&#x20;***(preceding‑sibling::tagname)* and put it like this:

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

This example demonstrates a **basic XPath expression using the *preceding-sibling* axis&#x20;**&#x74;o 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:**

```python
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.&#x20;**&#x59;ou create a Chrome browser instance using Selenium.
- **Opening the target page.&#x20;**&#x54;he *get()* method loads the form page.
- **Finding the label.&#x20;**&#x54;he 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

```xpath
//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

```xpath
//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,&#x20;**&#x6E;o matter how deep it may be buried.

## Common Errors With preceding-sibling

- **Not understanding DOM order.&#x20;**&#x50;receding in code means earlier in HTML, not necessarily above on the page.
- **Confusing tag names or node types.&#x20;**&#x42;e sure your elements match the correct names.
- **Using incorrect axes for the desired node.&#x20;**&#x55;tilizing *preceding-sibling* when you should be using *following-sibling.*

**Mistakes happen when navigating XML documents or HTML.&#x20;**&#x49;f 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 ](https://iproyal.com/blog/puppeteer-sharp-and-xpath-the-ultimate-web-scraping-guide/).

## 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 ](https://iproyal.com/blog/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](https://dashboard.iproyal.com/)

[Author](https://iproyal.com/blog/authors/marijus-narbutas/)

[Marijus Narbutas](https://iproyal.com/blog/authors/marijus-narbutas/)

[Senior Software Engineer](https://iproyal.com/blog/authors/marijus-narbutas/)

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 ](https://iproyal.com/blog/authors/marijus-narbutas/)[Meet all Writers](https://iproyal.com/blog/authors/)

Share on

### In This Article

- [Understanding the preceding-sibling Axis in XPath](https://iproyal.com/blog/xpath-preceding-sibling/#Understanding-the-preceding-sibling-Axis-in-XPath)
- [preceding-sibling vs following-sibling](https://iproyal.com/blog/xpath-preceding-sibling/#preceding-sibling-vs-following-sibling)
- [XPath Examples with Selenium](https://iproyal.com/blog/xpath-preceding-sibling/#XPath-Examples-with-Selenium)
- [Common Errors With preceding-sibling](https://iproyal.com/blog/xpath-preceding-sibling/#Common-Errors-With-preceding-sibling)
- [Conclusion](https://iproyal.com/blog/xpath-preceding-sibling/#Conclusion)
- [FAQ](https://iproyal.com/blog/xpath-preceding-sibling/#FAQ)

Ready to get started?

[Register now](https://dashboard.iproyal.com/register/)

Data News in Your Inbox

No spam whatsoever, just pure data gathering news, trending topics and useful links. Unsubscribe anytime.

No spam. Unsubscribe anytime.

## Related articles

[View all](https://iproyal.com/blog/category/tutorials/)

[Tutorials](https://iproyal.com/blog/the-most-common-payment-processing-obstacles-and-solutions-for-saas-companies/)

## [The Most Common Payment Processing Obstacles and Solutions for SaaS Companies](https://iproyal.com/blog/the-most-common-payment-processing-obstacles-and-solutions-for-saas-companies/)

[Here are the most common payment processing obstacles SaaS companies face and the solution that helped us focus on growth and our services!](https://iproyal.com/blog/the-most-common-payment-processing-obstacles-and-solutions-for-saas-companies/)

[August 8, 2022](https://iproyal.com/blog/the-most-common-payment-processing-obstacles-and-solutions-for-saas-companies/)

[3 min read](https://iproyal.com/blog/the-most-common-payment-processing-obstacles-and-solutions-for-saas-companies/)

[Tutorials](https://iproyal.com/blog/easy-programmatic-seo-with-wordpress/)

## [Easy Programmatic SEO with WordPress](https://iproyal.com/blog/easy-programmatic-seo-with-wordpress/)

[Our fourth and final entry in the pSEO series explains how to combine programmatic SEO with WordPress using plugins or code snippets.](https://iproyal.com/blog/easy-programmatic-seo-with-wordpress/)

[March 30, 2023](https://iproyal.com/blog/easy-programmatic-seo-with-wordpress/)

[10 min read](https://iproyal.com/blog/easy-programmatic-seo-with-wordpress/)

[Tutorials](https://iproyal.com/blog/api-for-dummies-the-coders-and-no-coders-guide/)

## [API for Dummies — The Coder’s and No-coder’s Guide](https://iproyal.com/blog/api-for-dummies-the-coders-and-no-coders-guide/)

[Learn API basics with this guide for coders and non-coders. Discover the different components of an API call.](https://iproyal.com/blog/api-for-dummies-the-coders-and-no-coders-guide/)

[June 1, 2023](https://iproyal.com/blog/api-for-dummies-the-coders-and-no-coders-guide/)

[11 min read](https://iproyal.com/blog/api-for-dummies-the-coders-and-no-coders-guide/)
