liveBook · Manning

Link text selector

As explained in BDD in Action, Second Edition

A link text selector locates a hyperlink by matching the text displayed for that link.

A link text selector locates a hyperlink by matching the text displayed for that link. In WebDriver acceptance tests, it provides a readable way to click links when stable identifiers such as id, name, or data attributes are unavailable. section 10.2.12

Choose the match according to how stable and specific the displayed text is:

  • Use an exact match for a link such as “Search”: driver.findElement(By.linkText("Search")).click(); section 10.2.12
  • Use a partial match when only part of the displayed text is needed, such as “Bookings” matching “My Bookings”: driver.findElement(By.partialLinkText("Bookings")).click(); section 10.2.12
  • Prefer another locator when displayed wording changes frequently or when the match is not sufficiently specific. section 10.2.12

Navigating a frequent-flyer site

A welcome page offers Search, My Bookings, and account links. An acceptance test can click Search using exact link text, or click My Bookings with a partial match. The selector remains intuitive but breaks if the displayed wording changes. section 10.2.12

How BDD in Action, Second Edition covers it

How deep it goes
Explains exact and partial matching with examples, but defers broader CSS and XPath locator strategies.
Examples
Uses Search and My Bookings links on a frequent-flyer welcome page as exact-match and partial-match examples.
What it assumes
Assumes basic HTML hyperlinks and WebDriver usage while explaining the locator technique through concrete UI examples.
What you can do after
Apply it via the WebDriver API to click links identified by exact or partial displayed text.
How it connects
Connects link text selectors with WebDriver, CSS selectors, XPath, and HTML data attributes.

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.