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:


  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 By.name(String 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;https://mail.google.com/mail/u/0/#inbox&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

4 thoughts on “Part 2: Ways of locating Web element using: Name, Class Name, Tag Name, Link Text

  1. Thanks for your response .
    Actually I wanted to ask will the linktext method compare spaces in between a text to get the match?
    And if we are not getting a proxy value using partial link text then how to locate the same?

    1. Link text will compare exactly the same including space, special characters etc. You can use other locator strategies as well to locate element with some text.

  2. Thanks for such a lovely post, below point can you clarify please as both are same , so how they are not matching?
    Link text must be matched char by char. If actual link text is as “Amod Mahajan” and you pass as “Amod Mahajan” in findElement() method, it will not locate element.

    And if we are getting proxy value using partial link text then what is the solution?

    1. Hi
      I didn’t get question properly.
      Regarding proxy value, you get a proxy value of web element in Page Object Model till you use that particular web element in program.

Leave a Reply