XPath/CSS Cheat Sheet

12 July 2024 | 4 min read

This cheat sheet provides a comprehensive overview of XPath and CSS selectors. It includes the most commonly used selectors and functions, along with examples to help you understand how they work.

This cheat sheet is available to download as a PDF file .

Sign up for 1000 free web scraping API credits and try these selectors for free.

How to copy an XPath selector from Chrome Dev Tools

  1. Open Chrome Dev Tools (press F12 key or right-click on the webpage and select "Inspect")
  2. Use the element selector tool to highlight the element you want to scrape
  3. Right-click the highlighted element in the Dev Tools panel
  4. Select "Copy" and then "Copy XPath"
  5. Paste the XPath expression into the code

Using Chrome developer tools to copy Target XPath

Xpath and CSS cheat sheet

Node Selection

Node selection allows you to select nodes based on their element names or hierarchy.

CSSXPathDescription
element//elementSelects all element nodes in the document.
/elementSelects the root element element.
parent element//parent/elementSelects all element nodes that are children of a parent node.
*//*Selects all elements in the document.

Example:

To select all div elements in a document:

  • CSS: div
  • XPath: //div

Position-based Selection

Position-based selection allows you to select elements based on their position within their parent or among their siblings.

CSSXPathDescription
:first-of-type[1]Selects the first element of its type among its siblings.
:nth-of-type(n)[n]Selects the nth element of its type among its siblings.
:last-of-type[last()]Selects the last element of its type among its siblings.
:first-child//*[1]Selects the first child element of its parent.
:last-child//*[last()]Selects the last child element of its parent.

Example:

To select the first p element:

  • CSS: p:first-of-type
  • XPath: //p[1]

Attribute Selection

Attribute selection allows you to select elements based on the presence or value of their attributes.

CSSXPathDescription
[attribute]//*[@attribute]Selects all elements with the specified attribute.
//element[@attribute]Selects all element nodes with the specified attribute.
[attribute="value"]//*[@attribute="value"]Selects all elements where the attribute is equal to "value".
//element[@attribute="value"]Selects all element nodes where the attribute is equal to "value".

Example:

To select all elements with a class of example:

  • CSS: [class="example"]
  • XPath: //*[@class="example"]

Attribute Value Matching

Attribute value matching allows you to select elements based on partial matches of attribute values.

CSSXPathDescription
[attribute^="value"][starts-with(@attribute, "value")]Selects elements where the attribute value starts with "value".
[attribute$="value"][ends-with(@attribute, "value")]Selects elements where the attribute value ends with "value".
[attribute*="value"][contains(@attribute, "value")]Selects elements where the attribute value contains "value".

Example:

To select elements with a class that starts with btn:

  • CSS: [class^="btn"]
  • XPath: [starts-with(@class, "btn")]

XPath Axes

Axes in XPath allow you to select nodes relative to the current node.

AxisXPath ExampleDescription
ancestor//div/ancestor::*Selects all ancestors (parent, grandparent, etc.) of the current node.
ancestor-or-self//div/ancestor-or-self::*Selects the current node and all of its ancestors.
child//div/child::*Selects all children of the current node.
descendant//div/descendant::*Selects all descendants (children, grandchildren, etc.) of the current node.
descendant-or-self//div/descendant-or-self::*Selects the current node and all of its descendants.
following//div/following::*Selects all nodes that appear after the current node in the document.
following-sibling//div/following-sibling::*Selects all siblings that appear after the current node.
parent//div/parent::*Selects the parent of the current node.
preceding//div/preceding::*Selects all nodes that appear before the current node in the document.
preceding-sibling//div/preceding-sibling::*Selects all siblings that appear before the current node.
self//div/self::*Selects the current node.

Example:

To select all children of a div element:

  • XPath: //div/child::*

XPath Operators & Functions

XPath includes various operators and functions to manipulate and evaluate node sets, strings, and more.

CategorySyntaxDescription
Comparison Operators= != < > <= >=Used for comparing values in predicates.
Logical Operatorsand or not()Combine or negate conditions in predicates.
Node Set Functionscount(node-set)Returns the number of nodes in the node-set.
position()Returns the position of the current node in the context of the current node list.
String Functionscontains(string1, string2)Checks if string1 contains string2.
starts-with(string1, string2)Checks if string1 starts with string2.
ends-with(string1, string2)Checks if string1 ends with string2.
substring(string, start, length)Extracts a substring from string.
string-length(string)Returns the length of string.
concat(string1, string2, ...)Concatenates strings.

Example:

To check if a string attribute contains the value example:

  • XPath: [contains(@attribute, "example")]

Further reading

image description
Harpal Singh

As a Software Engineer with experience in web technologies, I like developing scalable, high-performance solutions to solve business challenges. Driven by a passion for engineering and curiosity to learn, I regularly write on technical blogs.