In The Previous post of Java Selenium tutorial, we have already discussed a few of the locators like ID, Name and class name locators. Now in this post, we are going to discuss how to locate element by using the tag name locator. you can find out how to locate the elements with the help of other locators link you can find below.
How To Locate Element By Tag Name Locator In Selenium
In the previous post, we have seen “locators in Selenium“. In this post, we discuss “How To Locate Element By Tag Name Locator”. Find the below links on How to find elements on a web page using different types of locators.
- How To Locate Element By ID Locator
- How To Locate Element By Name Locator
- How To Locate Element By Class Name Locator
- How To Locate Element By Link Text/Partial Link Text Locator
- How To Locate Element By CSS Selector Locator
- How To Locate Element By XPath Locator
What is a tagName locator in Selenium?
Before starting how to find out an element with the help of using Tag name its better to understand whats a tag name?
The tag name is nothing but a part of the DOM structure, where you can see the elements on the page which is defined by any of the tags like input tag for input text box, button tag for buttons and anchor tags for the anchors.
The other locators like Name, ID and Class Name have multiple attributes like name, id and value class, etc. As for the ID, Name and class names locators in selenium, we use the values for locating the elements, but in the case of tagNamewe are use the tagName to identify the elements.
Some of the DOM Structures which are used to locate the elements In the Facebook Login Page:
<input type="email" class="inputtext" name="email" id="email" data-testid="royal_email"> Signup Button: <button type="submit" name="websubmit" id="u_0_15" class="_6j mvm _6wk _6wl _58mi _3ma _6o _6v">Sign Up</button>
Create A page link:
<a href="/pages/create/?ref_type=registration_form">Create a Page</a>
From the all discussed locators now you can think about when we are going to use this tag name locator in the Selenium webDriver. So the answer to these questions is supposed there is an element in the web page which does not have ID, class, or name and you need to locate the elements. In that case, we are going to use the tagName locator.
Note: When you try to find your required elements, then in that time if you have used tagName to locate the elements, you may get so many elements. So when you try to get an element that time it will fetch the first element.
I Hope with this tutorial and the above real-time examples, and you can able to get a good idea about how to use the tagName locator in selenium. If still if you have any doubts then let us know in the comment section.