Part 2: Ways of locating Web element using: Name, Class Name, Tag Name, Link Text

Hello Folks,

In last post , I explained about locating web element using ID. I explained last post in details so that you will be able to understand other locators easily.

We will learn in this post:

  1. Locating web element using name.
  2. Locating web element using tag name.
  3. Locating web element using link text.
  4. Locating web element using partial link text.
  5. Locating web element using class name.

Locating using name:

If any web element has name as an attribute, we can identify/locate that web element using its name. See below html code of email text box in Facebook login page.

<input id="email" class="inputtext" tabindex="1" name="email" type="email" value="" data-testid="royal_email" />

Remember, name is an attribute of html element. It must be “name” only. You can not take any name attribute. Same applies for attribute “id”.

To locate through selenium using name, syntax is below:

WebDriver driver= new FirefoxDriver();
WebElement ele= driver.findElement("email"))// Only first matching web element
List allEle= driver.findElements("email")) // all matching web elements


  1. By is a class in package “org.openqa.selenium”.
  2. It as static methods for each locators. We know static method of class can be called using class name. That is the reason we use as name) in findElement() and findElements() method.

It is possible that multiple web element can have same name. It may not be unique.

Locating using tag name:

HTML tags are the hidden keywords within a web page that define how your web browser must format and display the content.

Most tags must have two parts, an opening and a closing part. For example, <html>  is the opening tag and </html>  is the closing tag. Note that the closing tag has the same text as the opening tag, but has an additional forward-slash ( / ) character. I tend to interpret this as the “end” or “close” character.

&lt;a href=&quot;some link&quot;&gt;home&lt;/a&gt;

To locate through selenium using tag name, syntax is below:


This locator can be very useful to group web elements. If you want to know how many links are present on your webpage, you can use this locator.

List allLinks= driver.findElements(By.tagName(&quot;a&quot;));
System.out.println(&quot;Total links in currently loaded web page is: &quot;+allLinks.size());

Locating using Link Text:

We have an anchor(a) tag in HTML which is used to display a link on a web page. Within opening tag and closing tag of anchor, we can write some text as below:

&lt;a href=&quot;some link&quot;&gt;home&lt;/a&gt;

Here, “some link” is called as linktext. Using linktext also, we can locate web element.

Syntax in Selenium to locate web element using linktext:

WebElement link= driver.findElement(By.linkText(&quot;some link&quot;));
List&lt;WebElement&gt; allLinks= driver.findElements(By.linkText(&quot;some link&quot;));

1. Remember this locator is applicable only to anchor tag.
2. Link text must be matched char by char. If actual link text is as “Amod<space><space> Mahajan” and you pass as “Amod <space>Mahajan” in findElement() method, it will not locate element.
3. It is case sensitive as well. “amod” is not same as “Amod”.

Locating using partial Link Text:

With linkText we have problem that it should match char by char to locate. What is link text is dynamic. For example: In a mail site (Gmail), we have inbox and in braces it shows count of unread mails. Suppose we want to clink on inbox link. You can not pass exact link text as it will be keep changing when you read mail or new mail comes in.

&lt;a href=&quot;; target=&quot;_top&quot; class=&quot;J-Ke n0 aBU&quot; title=&quot;Inbox (598)&quot; aria-label=&quot;Inbox 1943 unread&quot; tabindex=&quot;0&quot;&gt;Inbox (598)&lt;/a&gt;

In above scenario, partialLinkText will help. There is no need to pass exact link text. We know “Inbox” will be constant always. So, we will pas just that as:


Note: If link text is constant use linkText locator. If it is dynamic, use partiallinkText locator.

But I have seen some problems with partialLinkText. It returns wrong/proxy elements as well. You can run below code and observe:

// To print all links which contains &quot;tion&quot; in link text
List&lt;WebElement&gt; all =driver.findElements(By.partialLinkText(&quot;tion&quot;));
for(WebElement e:all)
// passing exact link text
List&lt;WebElement&gt; all =driver.findElements(By.partialLinkText(&quot;Nutrition&quot;));
for(WebElement e:all)

Locating using class Name:
Like id, name html element may have an attribute named “class”.

&lt;input id=&quot;email&quot; class=&quot;inputtext&quot; tabindex=&quot;1&quot; name=&quot;email&quot; type=&quot;email&quot; value=&quot;&quot; data-testid=&quot;royal_email&quot; /&gt;

Syntax to locate web element using className:


That’s it guys. I will take dedicated post for cssSelector and xpath.
If you like my posts, please like, comment and share. Feedback and suggestions are always welcomed.


Author: Amod Mahajan