XPath Cheat Sheet

If you have a problem with referring to some element on an HTML page, and the only one solution if a customer needs to read some element properties in Iframe.

Selecting Unknown Nodes

Wildcard Description
* How to select any element node
@* How to select any attribute node
node() How to select any node of any kind

Selecting Nodes

Expression Description
nodename How to define all nodes with the name “nodename”
/ How to selects from the root node
// Selects nodes in the document from the current node that match the selection no matter where they are
. How to define the current node
How to select the parent of the current node
@ How to select attributes

XPath Syntax

Most useful methods :

  1. Contains() method:

Syntax: To find the “Log in to Twitter” web element, use any of the following XPath expressions that include contains() method.

By Text

* //h1[contains(text(),’ Log in to’)]
* //h1[contains(text(),’ in to Twitter’)]

  1. Text() method:

Syntax: To find the “Log in to Twitter” web element, use the following XPath expression that has the text() method.

* //h1[text()=’Log in to Twitter’]

In this expression, we use the absolute text that is present between the start tag<h1> and the end tag </h1>. If we use the text() function with partial Xpath as we do in contains() and starts-with(), we wouldn’t be able to find the element.

Here is a list of all functions

1 Like