Selenium: Locators and their Usage: Part -2

As you may know from the last blog post, Selenium Locator is used in various ways. Therefore, we can see that Selenium's web driver provides different locators to find web elements on web pages. It is just a matter of considering whether the locator is a one-to-one element match or not.

TagName

TagName locator is used to identify the web elements on the webpage by matching the specified Tag names and helps to extract the content within the tag.
Syntax:
tagNameLocator("Tag_Name")
Example:
Here we used the Product Market Fit button example to demonstrate how the tagName  locator can be used in 'Selenium WebDriver' to locate and automate the web element. We can select the tag name locator during code, only if the tag name is unique. This is only an example.
Image:

Description:
In the above image, we see the tagName as button in the DOM structure inside the dev-tool. We used  By.tagName() a method in selenium code and have passed the button as a value which is the tag name of Product Market Fit button by which we can locate the element on the web page and perform a click action on it.
Code snippet:

driver.findElement(By.tagName("button")).click();

LinkText

The linkTextlocator helps in identifying hyperlinks on a web page with  <a> tags ( which extract the hyperlinks from within) and using the text of that hyperlink.
Syntax:
linkTextLocator("Particular_Link_Text")
Example:
Here we used the blog link text example to demonstrate how the linkText locator can be used in ‘Selenium WebDriver’ to locate and automate the web element.
Image:

Description:
In the above image, we can see the anchor tag <a> having hyperlink href="https://plgworks.com/blog/" and linkText value BLOG. We used the By.linkText() method and have passed the BLOG as a value for selected web elements and taking a click action on the web page.
Code snippet:

driver.findElement(By.linkText("BLOG")).click();

PartialLinkText

PartialLinkText locator is similar to the linkText locator. It is also used for locating hyperlinks present in HTML for particular web elements. By using PartialLInkText we can locate elements using a portion of text if the web element link text is long.
Syntax:
findElement(By.partialLinkText(“partialLinkText”))
Example:
Here we used the Talk to us link text example to demonstrate how the partialLinkText  locator can be used in 'Selenium WebDriver' to locate and automate the web element.                          
Image:    

Description:
In the above image, we can see the anchor tag <a> having hyperlink and partialLinkText value TALK TO US, we used the By.partialLinkText() method and have passed the TALK as a ‘value' to selected web element and taking a click action on the web page. PartialLinkText can only be used when part of the text is to be matched, see the highlighted portion in the above image.  
Code snippet:

driver.findElement(By.partialLinkText("TALK")).click();

We will cover the other two remaining locators XPath and CSS selector  in the next blog.  

Ganesh Chavan

Ganesh Chavan

QA Test Engineer at PLGWorks.com