Bytes
Web Development

XPath Cheat Sheet: XPath Cheat Sheet With Examples

Last Updated: 6th February, 2025
icon

Jay Abhani

Senior Web Development Instructor at almaBetter

Master XPath with this ultimate XPath Cheat Sheet! Learn syntax, locators, axes, queries, and examples for Selenium, XML, and web scraping; all in one place

XPath (XML Path Language) is a versatile and powerful tool used to navigate through and extract data from XML and HTML documents. This cheat sheet covers everything from basic XPath syntax to advanced techniques for Selenium automation, web scraping, and XML parsing.

What is XPath?

XPath is a query language designed to extract and locate information within an XML or HTML document. It is the backbone of many automation and web scraping tools like Selenium, Puppeteer, and BeautifulSoup.

XPath Syntax Cheat Sheet

XPath Syntax Overview

XPath expressions define paths to navigate through a document's nodes. The two primary types of paths are:

Absolute XPath: Starts from the root node.

/html/body/div

Relative XPath: Starts from the current node or anywhere in the document.

//div[@class='example']

Common XPath Expressions

Select all nodes of a type:

//tagname

Select by attribute:

//tagname[@attribute='value']

Select by partial text:

//*[contains(text(), 'partial')]

Select using functions:

//*[starts-with(@attribute, 'value')]

Examples:

Select all div elements:

//div

Select a tags with href attributes:

//a[@href]

XPath Locators Cheat Sheet

What are XPath Locators?

Locators help identify elements in a document. XPath locators are essential for automation frameworks like Selenium.

Locators for Selenium

Find by ID:

//*[@id='elementID']

Find by Class Name:

//*[@class='className']

Find by Text:

//*[text()='Sample Text']

Examples:

Locate a button by text:

//button[text()='Submit']

Locate an element with multiple conditions:

//input[@type='text' and @name='username']

XPath Axes Cheat Sheet

Introduction to XPath Axes

Axes define relationships between nodes. Common axes include:

Child Axis: Selects children of a node.

child::tagname

Parent Axis: Selects the parent of a node.

parent::tagname

Following Sibling Axis:

following-sibling::tagname

Examples:

Select the first child:

/html/body/div/child::p

Select all ancestors:

ancestor::*

XPath Selector Cheat Sheet

XPath vs CSS Selectors

Feature

XPath

CSS Selectors

Navigate backwardYesNo
Complex ConditionsYesLimited

Examples

XPath:

//div[@id='example']

CSS:

div#example

XPath Query Cheat Sheet

Writing Efficient Queries

Use contains for dynamic content:

//*[contains(@class, 'dynamic')]

Real-World Examples

Extract product prices:

//span[@class='price']

XPath Cheatsheet with Examples

Beginner-Friendly Examples

Select all links:

//a

Select input fields:

//input[@type='text']

Advanced Examples

Combine multiple conditions:

//div[@class='class1' and contains(@id, 'partial')]

Selenium XPath Cheat Sheet

XPath in Selenium

Locate an element:

driver.find_element_by_xpath("//div[@id='example']")

Locate multiple elements:

driver.find_elements_by_xpath("//a")

XPath CSS Cheat Sheet

Examples of CSS vs XPath

XPath:

//div[@class='example']

CSS:

div.example

XML XPath Cheat Sheet

Handling Namespaces

Use local-name() for namespace-agnostic queries:

//*[local-name()='tagname']

XPath Injection Cheat Sheet

What is XPath Injection?

XPath Injection occurs when user input is unsanitized and directly included in XPath queries.

Examples and Prevention

Vulnerable Query:

//user[username='input' and password='input']
  • Secure Query: Use parameterized queries and input validation.

Conclusion

This XPath cheat sheet serves as a comprehensive guide for navigating and querying XML/HTML documents effectively. Whether you're using XPath for Selenium automation, web scraping, or XML processing, the concepts, examples, and techniques discussed here will help you master XPath quickly.

More Cheat Sheets and Top Picks

  • Official Address
  • 4th floor, 133/2, Janardhan Towers, Residency Road, Bengaluru, Karnataka, 560025
  • Communication Address
  • Follow Us
  • facebookinstagramlinkedintwitteryoutubetelegram

© 2025 AlmaBetter