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

This entry is part 37 of 39 in the series Selenium

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:

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:

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:

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,

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

Left half of this image shows the entered phone number and 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!

Series Navigation<< 9j. WebDriver – Locating elements: Part 1 (by id, name, tagName)9l. WebDriver – Locating elements: Part 3a (by cssSelector) >>

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

Leave A Comment

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