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
