Write an XPath
  • 31 Mar 2020
  • 1 Minute To Read
  • Contributors
  • Print
  • Share
  • Dark
    Light

Write an XPath

  • Print
  • Share
  • Dark
    Light

XPath lets you specify an HTML element or a set of HTML elements. This article provides the basic elements you need to get started, but keep in mind that there are many ways to write an XPath. Take the time to practice and find a method that works for you.

How to check XPath in HTML

  1. Right-click an item on the web page.
  2. Select Inspect.
  3. Press Ctrl+F to search for the XPath match.

XPath Syntax has three elements

  • Axis: Specifies the tree relationship and the direction to navigate from node to node.
  • Node test: The name of the HTML element you want to select.
  • Zero or more predicates: Filters a node to refine results. These can be functions, numbers, or operators within square brackets.

Writing XPath

When you want to write a custom XPath for a capture action, hover over the element you want to select, then right-click and select Inspect to open the HTML document that corresponds to the element.

Take your time to become familiar with the website and the HTML. Look for similarities between the HTML and the text on the website.

Let's simplify the HTML element as if you were searching for a family tree for a specific person. To write your first XPath, specify the axis and name, then add a filter in brackets.

image.png

We want to search all HTML elements , so we'll set our axis to descendant-or-self using the short-hand "// ." The name of the element is "travis." To filter out any other potential matches, we'll add a filter using the attribute of hair that is equal to brown.

Screen Shot 2020-03-24 at 9.45.28 PM.png

Practice:
Write an XPath to select the div below.

Screen Shot 2020-03-24 at 9.51.49 PM.png







answer: //div[@id="mozenda"]

Was This Article Helpful?