We use cookies to analyze site traffic and show personalized ads. You can accept all cookies or decline personalized advertising.
Learn more in our Privacy Policy.
Find hyperlinks using their visible text.
In this lesson, you’ll learn how to locate links (hyperlinks) using the text that users actually see on the page. These strategies only work with <a> elements.
By.linkText("Exact Text")By.partialLinkText("Partial")Both of them look at the visible text between <a> and </a>.
What it does
Finds an <a> element whose visible text exactly matches the given string.
Example: By.linkText("Selenium Official Page") matches <a>Selenium Official Page</a>.
When to use it
What it does
Finds an <a> element whose visible text contains the given substring.
Example: By.partialLinkText("Documentation") matches <a>Selenium WebDriver Documentation</a>.
When to use it
Links demo (linkText & partialLinkText)
Learn more about Selenium:
Click the links to see how they would work in a real browser!
1234567WebElement officialPageLink = driver.findElement(By.linkText("Selenium Official Page"));
officialPageLink.click();
WebElement docsLink = driver.findElement(By.partialLinkText("Documentation"));
docsLink.click();
driver.quit();<a> element whose visible text is exactly "Selenium Official Page".<a> element whose visible text contains the word "Documentation". Matches "Selenium WebDriver Documentation".If you know the exact, stable text of the link, By.linkText(...) is more precise and less risky.
Make sure the partial text is unique enough. If you use a common word like "Click", Selenium might match the wrong link.
If your UI team often tweaks link labels, consider using more stable locators (like IDs) instead of link text.