Skip to content
CodeKruPro

Find Element by Link Text

8/70

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

About this lesson

By.linkText in Selenium Java: find a link by its exact text

Selenium Java AssignmentsBeginnerLocators

How it works

By.linkText("text") finds an a element whose visible text equals the string you pass, exactly and case-sensitively. It only ever matches links, so it cannot be confused by a button or a heading with the same words.

The match uses rendered text, not the HTML source, so a link that is not displayed never matches. The Links page shows this: its sidebar contains a link labelled Buttons, but in the default 780x580 headless window the whole sidebar is pushed off-screen, so By.linkText("Buttons") finds nothing. In a 1920x1080 window the same locator returns the sidebar link.

Text locators read the page the way a user does, which makes tests easy to follow. The price is that wording changes and translations break them.

About the assignment

The Links page lists links with distinct labels. You will locate two of them by their full text, Go to Homepage and Visit Google Website, and print each one's id to prove which element was found.

What you will do

  1. Find the first link with driver.findElement() and By.linkText(), using its full text.
  2. Print its id attribute after the label Link 1 ID:.
  3. Repeat for the Google link and print its id after the label Link 2 ID:.

Common mistakes

  • Wrong capitalisation

    By.linkText("go to homepage") throws NoSuchElementException; the match is case-sensitive.

  • Passing only part of the text

    By.linkText("Go to") also throws NoSuchElementException. Matching a fragment needs By.partialLinkText(), the next lesson.

  • Links without text

    The image link on this page wraps an img and has no text at all, so no link text locator can reach it. Use its id or a CSS selector.

In a real test suite

Link text suits navigation menus and footers where the label is part of the requirement, for example checking that Privacy Policy is present. When reading where a link points, note that getAttribute("href") returns the full resolved address, while getDomAttribute("href") returns the value as written: the homepage link here gives /.

When labels are long or contain changing parts, lesson 9 matches only a fragment of the text.

To try one of these calls on any page without a task, open the online Selenium compiler for Java.

Starter code

The editor above opens with this code. Fill in the TODO lines, then run and submit it.

Main.java
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

public class Main {
    public static void main(String[] args) throws Exception {
        WebDriver driver = new ChromeDriver();
        driver.get("https://testkru.com/Elements/Links");
        
        // TODO: Find link by text "Go to Homepage" and print its ID
        // TODO: Find link by text "Visit Google Website" and print its ID
        
        driver.quit();
    }
}