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.

Code:

driver.findElement(By.className("i18n_phone_number_input-inner_input"));

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.

Code:

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’.

Code:

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

Locating by partialLinkText

Overall picture

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

Scenario

  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;

		@Before
		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";
		}
		
		@Test
		public void testPageTitle() throws Exception{
			// Open baseUrl in Firefox browser window
			driver.get(baseUrl);
			// 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
			mobileNum.clear();
			// Enter/type the value to the text box
			mobileNum.sendKeys("9496543210");
			// Locate 'Learn more' hyperlink by link text
			WebElement link1 = driver.findElement(By.linkText("Learn more"));
			// Click on 'Learn more'
			link1.click();
			// 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());
		}
		
		 @After
		  public void tearDown() throws Exception{
			// Close the Firefox browser
			driver.close();
		}
}

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.

Output

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!

2 Comments

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.