Summary -

In this topic, we described about the below sections -

LinkText strategy used to locate the element using the link text as in hyperlinks. There is no standard for uniqueness of the linkText on a web page and multiple elements can have same linkText. So, there is a chance that multiple links may have the same linkText. In this scenario, the first link that matches with the linkText selected for testing. linkTexts are always prefixed with the anchor tag (<a> tag) and can easily findout by using anchor tag.


  • Can only select anchor elements.
  • Useful when testing navigation.


  • We must find the text of the link before using it.


Now, let’s understand the working of linkText locator with the help of a simple example. We will launch chrome and navigate to Here, we will try to locate the gmail link using linkText locator.

Link Text

On inspecting the above web element, we can see an anchor tag (<a>) has the text Gmail. Now, we will use the value in linkText locator i.e. Gmail to open gmail login page.

Link Text

Let’s see how the automation of the search look like in Selenium IDE that navigates to gmail login page using linkText locator.

Link Text

The java program for the above test is -

// Generated by Selenium IDE import org.junit.Test; 
import org.junit.Before; import org.junit.After; 
import static org.junit.Assert.*; 
import static; 
import static org.hamcrest.core.IsNot.not; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.remote.RemoteWebDriver; 
import org.openqa.selenium.remote.DesiredCapabilities; 
import org.openqa.selenium.Dimension; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.interactions.Actions; 
import org.openqa.selenium.JavascriptExecutor; 
import org.openqa.selenium.Alert; 
import org.openqa.selenium.Keys; 
import java.util.*; 
public class LocatorsTest {   
    private WebDriver driver;   
    private Map<String, Object> vars;   
    JavascriptExecutor js;   

    public void setUp() {     
        driver = new ChromeDriver();     
        js = (JavascriptExecutor) driver;     
        vars = new HashMap<String, Object>();   
    public void tearDown() {    
    public void locators() {     
        driver.manage().window().setSize(new Dimension(1134, 705));    
            WebElement element = driver.findElement(
            Actions builder = new Actions(driver);       
            WebElement element = driver.findElement(
            Actions builder = new Actions(driver); 
            builder.moveToElement(element,  0, 0).perform();

When you run the java program, Chrome driverlaunches chrome, redirect to,clicks on the link text "Gmail" to open gmail login page.Refer the below image for the output -

Link Text

The above example gives a clear understanding of how linkText locator in Selenium works.