Skip to content
CodeKruPro

Match a link by its exact text

Automation taskEasyAsked in: Accenture, Capgemini, TCS, Wipro

"How do you click the Logout link?" The link text locator matches the full visible text of a link (an <a> element). It works only on links, and the text must match exactly. Here you find two links by their exact text and print the id of each.

Find Element by Link Text

Video walkthrough

Watch on YouTube

Find Element by Link Text

The By.linkText() locator finds an <a> element using its exact visible text.

On the Links page, there are several links including:

  • Go to Homepage (id: simpleLink)
  • Visit Google Website (id: googleLink)

Task:

  1. Navigate to https://testkru.com/Elements/Links
  2. Find the link with exact text Go to Homepage
  3. Print its ID
  4. Find the link with exact text Visit Google Website
  5. Print its ID

Expected output:

Link 1 ID: simpleLink
Link 2 ID: googleLink