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
- Find the first link with
driver.findElement()andBy.linkText(), using its full text. - Print its
idattribute after the labelLink 1 ID:. - Repeat for the Google link and print its
idafter the labelLink 2 ID:.
Common mistakes
Wrong capitalisation
By.linkText("go to homepage")throwsNoSuchElementException; the match is case-sensitive.Passing only part of the text
By.linkText("Go to")also throwsNoSuchElementException. Matching a fragment needsBy.partialLinkText(), the next lesson.Links without text
The image link on this page wraps an
imgand 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.
Related lessons
- Find Element by Partial Link TextMatch a fragment of the link text instead of the whole label.
- XPath Functions - contains() and starts-with()XPath with
contains()matches text on any element, not only links. - Get Attribute Value with getAttribute()Read
hrefand other attributes from the link you found.
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.
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();
}
}