XPath syntax and examples
  • 25 May 2021
  • 2 Minutes to read
  • Contributors
  • Dark
    Light
  • PDF

XPath syntax and examples

  • Dark
    Light
  • PDF

Article Summary

An XPath expression follows the structure below.

  • Axis: The direction of where to travel. This means moving either up or down the heirarchy of the HTML .
  • Element name: The name of the item you are specifying
  • Predicates: Filters to refine your results. These are added between two square brackets.

Below is a list of common functions you may see while writing an XPath expression. There are many more combinations and expressions that can be used, so you may discover something new.

XPath 1.0 is the standard used in the Agent Builder.

Location path :

There are two types of XPath expressions, absolute and relative.

Absolute: An absolute XPath is a complete path from the root element (<html>) to the desired element. Changes to the structure of a page can cause absolute XPath expressions to break.

Relative: A relative XPath locates the desired element in relation to another element that you specify. Because relative XPath expressions reference a single point in the document rather than a complete path, they're more resistant to site changes.

XPath expression syntax

ExpressionSyntaxDefinition
Attribute@Specifies the selection of an attribute of an element.
Descendants//Select all of the children of the context node, and all of their children, etc.
Attribute selector//elementname[@attribute="value"]Select an element with an exact match for both the attribute and value.
Attribute contains function//elementname[contains(@attribute,"value")]The contains function has an ability to find the element with partial attribute match.
contains text function//elementname[contains(.,"text")]Selects the element with a partial text match.
and/or function//elementname[(contains(@attribute,"value")][@attribute="value")] //elementname[(contains(@attribute,"value") or @attribute="value")]Either seperate or combine predicates. Apply and/or to eliminate or expand the scope of the selected element.
Normalized-space//div[normalize-space(@class)='foobar'] //div[contains(normalize-space(@class),'foobar')]Removes leading and trailing white-space from a string, replaces sequences of whitespace characters by a single space, and returns the resulting string.
child//startingElement/child::elementname //elementname/elementnameSelects the direct child of the context element. If an XPath expression does not specify an axis, the child axis is understood by default
parent../ example in an expression: //elementname/../parent::*Selects the parent element of the current element.
ancestor//startingElement/ancestor::elementnameSelects the furthest ancestor of the element that you specified. Specify which sibling by adding the element name after the axis.
following-sibling//startingElement/following-sibling::elementnameSelects all siblings after the current element. Specify which sibling by adding the element name after the axis.
preceding-sibling//elementname/preceding-sibling::elementnameAxis that indicates all the elements with the same parent which appear before the context element in the HTML.
Concatenate//elementname[@attribute="value"] | //elementname[@attribute="value"]Selects all matching elements and combines their values into a single string. Reformat the field or add a delimiter with Refine Capture Text.
Last sibling or child//elementname/elementname[position()last()] //elementname/elementname[position()last()-1]Selects the specific element based on postion from the current selected element and axis.
Field reference//elementname[contains(.,"%FieldName%")]Dynamically change the value inside of your XPath using a field variable.

Was this article helpful?