liveBook · Manning

XPath

As explained in BDD in Action, Second Edition

An XPath expression is a path query that selects nodes in a structured document such as an HTML or XML document.

An acceptance test may need to find more than a button with a known ID. It might need to check a destination’s displayed price or click a registration option identified by its visible label. XPath selects elements in an HTML page by their position, attributes, or text; in WebDriver, pass the expression to `By.xpath()`. section 10.2.12 section 10.2.13

Start with a CSS selector when it identifies the element clearly. XPath is useful when that selector runs into limits, particularly when the test needs to follow the page structure from one element to another. The trade-off is that XPath generally requires more knowledge of that structure and can be more verbose for simple selections. section 10.2.12

Choose an expression that matches what the test needs to identify:

  • `//h3[@id='welcome-message']` finds a heading by its ID; use it with `driver.findElement(By.xpath(...))` when one element is expected. section 10.2.12
  • `//span[@class='destination-title']` matches an exact class attribute. `//*[contains(@class,'destination-title')]` allows other class text and does not require a particular element name. section 10.2.12
  • `//label[.='Window']` finds a label by its text, allowing WebDriver to click the label associated with a radio-button choice. section 10.2.13

Find the price beside a destination

Given a destination entry containing `<span class="destination-title">Singapore</span>` and a sibling price span, `//span[.='Singapore']/../span[contains(@class,'destination-price')]` starts at the title, moves to its parent, and selects the price within that entry. This is more targeted than finding every price on the page. section 10.2.12

Select a registration preference

A registration step can construct `String seatPreference = String.format("//label[.='%s']", newMember.getSeatPreference());` and then call `driver.findElement(By.xpath(seatPreference)).click();`. The label text comes from the persona used by the step, so the same interaction can select the corresponding preference. section 10.2.13

Table 10.3 Useful XPath expressions (view table figure)

XPath expression

Example

Notes

node

a

Matches all of the <a> elements

//node

//button

Matches all of the <button> elements somewhere under the document root

//node/node

//button/span

Matches <span> elements that are situated directly under a <button> element

[@attribute=value]

//a[@class='navbar-brand']

Matches <a> elements whose class attribute is exactly equal to navbar-brand

[contains(@attribute,value)]

//div[contains(@class,'navbar-header')]

Matches <div> elements whose class attribute contains the expression navbar-header

node[n]

//div[@id='main-navbar']//li[3]

Matches the third <li> inside the <div> with an id of main-navbar

[.=value]

//h2[.='Flying High Frequent Flyers']

Matches the <h2> element with text contents equal to Flying High Frequent Flyers

Compare paths, attribute predicates, positions, and exact-text matches when choosing a locator. section 10.2.12

How BDD in Action, Second Edition covers it

How deep it goes
Explains common expressions through WebDriver calls and element examples, without developing XPath beyond the patterns needed for page interaction.
Examples
Uses destination titles and prices, a welcome heading, and registration preference labels as selection targets.
What it assumes
Assumes familiarity with HTML elements and WebDriver calls while introducing XPath expression patterns through examples.
What you can do after
Apply it via WebDriver `By.xpath()` to find elements by attributes or text and navigate to related elements.
How it connects
Connects XPath to CSS selectors, HTML document structure, WebDriver, and UI acceptance tests.

Also explained in

Compare how 3 books explain XPath →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of BDD in Action, Second Edition. The section links point to the parts of the book it draws on.