9k. WebDriver – Locating elements: Part 2 (by className, linkText, partialLinkText)

Hiya friends! Let us dive deeper today to look at a few more strategies for locating elements. In this post we will concentrate on locating by,

  • className
  • linkText
  • partialLinkText

Locating by className

Class name is nothing but the CSS class name that is used to style a web element. It is important to note that numerous web elements on the page may have the same className. In that case, findElements method can be used and the result can be indexed. Refer locating by tagName strategy (explanation along with an example provided in the previous post). If we have an element with a unique className or if the element under test is the first in that page using that className, then findElement would do the task.

Syntax: driver.findElement(By.className(“element_class_name”));

Explanation: Locates the first element with matching CSS class name.

Example: Let us locate the ‘Mobile phone’ text box of the gmail account creation page.

Right click on the text box and select inspect element to get the corresponding HTML code. We can see that the ‘input’ tag contains class=”i18n_phone_number_input-inner_input”. Let us go ahead and locate ‘Mobile phone’ text box using this class name for further interaction.



Locating by ClassName

Locating by linkText

linkText is very useful when you want to interact with hyperlinks. The actual text displayed on the web page for that link is used. How easy is that?

Syntax: driver.findElement(By.linkText(“hyperlink_text”));

Explanation: Locates the first hyperlink with matching link text.

Example: Let us locate the hyperlink, ‘Learn more’ given at the bottom of the gmail account creation page.


driver.findElement(By.linkText("Learn more"));

Locating by LinkText

Locating by partialLinkText

PartialLinkText is also used to interact with hyperlinks and is very similar to linkText locating strategy. Instead of providing the complete text displayed for the link, this method does a partial match. Hence a part of the link text can be given as the matching criteria.

Syntax: driver.findElement(By.partialLinkText(“hyperlink_partial_text”));

Explanation: Locates the first hyperlink which contains the specified partial link text.

Example: Let us locate the hyperlink, ‘I prefer to use my current email address’ below the ‘Choose your username’ text box of the Gmail account creation page by just providing the sub-text, ‘I prefer to’.


driver.findElement(By.partialLinkText("I prefer to"));

Locating by partialLinkText

Overall picture

Let us see a test case implementing the above three locator types.


  1. Open Firefox browser.
  2. Navigate to google account creation page
  3. Locate mobile phone text box by className
  4. Enter ‘9496543210’ as the mobile number
  5. Locate ‘Learn more’ hyperlink by linkText
  6. Clink the hyperlink
  7. Locate ‘I prefer to use my current email address’ hyperlink with partialLinkText
  8. Print the complete link text to console for verification

JUnit code for this scenario is,

import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class ElementLocatorTest2 {
		//Declaring variables
		private WebDriver driver; 
		private String baseUrl;

		public void setUp() throws Exception{
			// Selenium version 3 beta releases require system property set up
			System.setProperty("webdriver.gecko.driver", "E:\\Softwares\\"
					+ "Selenium\\geckodriver-v0.10.0-win64\\geckodriver.exe");
			// Create a new instance for the class FirefoxDriver
			// that implements WebDriver interface
			driver = new FirefoxDriver();
			// Implicit wait for 5 seconds
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
			// Assign the URL to be invoked to a String variable
			baseUrl = "https://accounts.google.com/SignUp";
		public void testPageTitle() throws Exception{
			// Open baseUrl in Firefox browser window
			// Locate Mobile phone text box by className and
			// assign it to a variable of type WebElement
			WebElement mobileNum = driver.findElement(By.className("i18n_phone_number_input-inner_input"));
			// Clear the default placeholder or any value present
			// Enter/type the value to the text box
			// Locate 'Learn more' hyperlink by link text
			WebElement link1 = driver.findElement(By.linkText("Learn more"));
			// Click on 'Learn more'
			// Locate hyperlink by partial link text
			WebElement link2 = driver.findElement(By.partialLinkText("I prefer to"));
			// Printing the complete link text to console
			System.out.println("Complete link text: " + link2.getText());
		  public void tearDown() throws Exception{
			// Close the Firefox browser

Execution result

The comments are clearly provided for each line of code and hence self-explanatory.

Console output

In JUnit pane, green bar shows that the test case is executed successfully. Output is printed to the console confirming that the hyperlink, ‘I prefer to use my current email address’ is accessed by just providing the sub-text, ‘I prefer to’ as the partialLinkText.


The left half of this image shows the entered phone number and the right half shows the final output executed in Firefox browser. As the ‘Learn more’ link is clicked, we are redirected to the corresponding page.

Time for another break friends. In our following post get ready to digest more information as we will look at two of the effective techniques for locating elements.

Enjoy the day!

Lakshmi Chandana is a Software professional + passion-fueled blogger + novel-reader + artist + tutor to make your day a little brighter than it was before! She is thrilled you are here! She is on a mission to make sure learning sticks but with the fun part kept intact. She uses certain tricks called BrainBells (inspired from barbells and dumbbells used for workout) to achieve this and she says, “this is not the hardest job! As once minions said, it for sure is working in a bubble wrap factory. Imagine the self-control needed!” So, dive in to explore the fun-filled World of Learning!!

2 thoughts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to top Search