<div class="selenium-notes">
<h1>Tag Name Locator in Selenium</h1>
<p><strong>Tag Name Locator</strong> is one of the locator strategies provided by Selenium WebDriver for identifying web elements using their HTML tag name. Every HTML element is represented using a tag such as <code>input</code>, <code>button</code>, <code>a</code>, <code>h1</code>, <code>p</code>, <code>div</code>, <code>table</code>, or <code>select</code>. Selenium can use these tag names to locate elements on a web page.</p>
<p>Tag Name is especially useful when multiple elements share the same HTML tag and the automation script needs to retrieve a collection of elements using <code>findElements()</code>. It can also be used with <code>findElement()</code> when the tag uniquely identifies the required element.</p>
<p>JustAcademy includes locators and WebDriver element interaction as part of its Selenium automation training curriculum. You can explore the <a href="https://www.justacademy.co/course-detail/selenium-training" target="_blank" rel="noopener noreferrer">JustAcademy's Selenium Training Course</a> and use the <a href="https://www.justacademy.co/register-for-course-demo" target="_blank" rel="noopener noreferrer">Register for Course Demo</a> link for course-related information.</p>
<hr>
<h2>1. What is a Tag Name?</h2>
<p>A tag name is the name of an HTML element used to define the structure and content of a web page. For example, an input field uses the <code><input></code> tag, a button uses the <code><button></code> tag, and a hyperlink uses the <code><a></code> tag.</p>
<pre><code><input type="text" name="username">
<button type="submit">Login</button>
<a href="/home">Home</a>
<p>Welcome to Selenium</p></code></pre>
<p>In this example, the tag names are <code>input</code>, <code>button</code>, <code>a</code>, and <code>p</code>.</p>
<hr>
<h2>2. What is Tag Name Locator in Selenium?</h2>
<p>Tag Name Locator allows Selenium WebDriver to locate an HTML element by specifying its tag name.</p>
<p>The Selenium Java syntax is:</p>
<pre><code>driver.findElement(By.tagName("tagName"));</code></pre>
<p>For example:</p>
<pre><code>driver.findElement(By.tagName("input"));</code></pre>
<p>This statement searches the current web page for an element whose HTML tag is <code>input</code>.</p>
<hr>
<h2>3. Basic Syntax</h2>
<pre><code>By.tagName("tagName")</code></pre>
<p>Example:</p>
<pre><code>WebElement element = driver.findElement(By.tagName("button"));</code></pre>
<p>Here Selenium searches for an HTML element with the <code>button</code> tag.</p>
<hr>
<h2>4. Why Use Tag Name Locator?</h2>
<p>Tag Name Locator is useful when the HTML tag itself provides enough information to identify the element or when the automation script needs to retrieve multiple elements of the same type.</p>
<ul>
<li>It is simple to understand.</li>
<li>It works with standard HTML tags.</li>
<li>It is useful for locating groups of elements.</li>
<li>It is commonly used with <code>findElements()</code>.</li>
<li>It can be useful for counting elements.</li>
<li>It can help retrieve all links, buttons, headings, paragraphs, images, or input fields.</li>
</ul>
<hr>
<h2>5. Selenium Method Used with Tag Name</h2>
<p>The main Selenium method used with Tag Name is <code>findElement()</code>.</p>
<pre><code>WebElement element = driver.findElement(By.tagName("input"));</code></pre>
<p>To locate multiple elements, use <code>findElements()</code>.</p>
<pre><code>List<WebElement> elements = driver.findElements(By.tagName("input"));</code></pre>
<hr>
<h2>6. Setting Up Selenium</h2>
<p>The following imports are commonly required when using Tag Name Locator with Selenium Java.</p>
<pre><code>import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.List;</code></pre>
<hr>
<h2>7. Complete Basic Example</h2>
<pre><code>import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class TagNameExample {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
WebElement heading = driver.findElement(By.tagName("h1"));
System.out.println(heading.getText());
driver.quit();
}
}</code></pre>
<p>In this example, Selenium searches for the first <code>h1</code> element on the page and retrieves its text.</p>
<hr>
<h2>8. HTML Example for Tag Name Locator</h2>
<pre><code><html>
<body>
<h1>Login Page</h1>
<p>Enter your credentials</p>
<input type="text">
<button>Login</button>
</body>
</html></code></pre>
<p>Possible tag names in this page include:</p>
<table border="1" cellpadding="8" cellspacing="0">
<tr><th>HTML Element</th><th>Tag Name</th></tr>
<tr><td>Heading</td><td>h1</td></tr>
<tr><td>Paragraph</td><td>p</td></tr>
<tr><td>Input</td><td>input</td></tr>
<tr><td>Button</td><td>button</td></tr>
</table>
<hr>
<h2>9. Locate an Input Field Using Tag Name</h2>
<p>If a page contains an input field, Selenium can locate it using the <code>input</code> tag.</p>
<pre><code>WebElement input = driver.findElement(By.tagName("input"));</code></pre>
<p>However, if multiple input fields exist, <code>findElement()</code> normally returns the first matching element. Therefore, Tag Name alone may not be sufficient to identify a particular input field.</p>
<hr>
<h2>10. Locate a Button Using Tag Name</h2>
<pre><code>WebElement button = driver.findElement(By.tagName("button"));
button.click();</code></pre>
<p>This searches for the first <code>button</code> element and clicks it.</p>
<hr>
<h2>11. Locate a Heading Using Tag Name</h2>
<pre><code>WebElement heading = driver.findElement(By.tagName("h1"));
System.out.println(heading.getText());</code></pre>
<p>This approach is useful when a page contains a unique heading.</p>
<hr>
<h2>12. Locate a Paragraph Using Tag Name</h2>
<pre><code>WebElement paragraph = driver.findElement(By.tagName("p"));
System.out.println(paragraph.getText());</code></pre>
<p>This retrieves the text of the first paragraph element.</p>
<hr>
<h2>13. Locate a Link Using Tag Name</h2>
<p>HTML hyperlinks generally use the <code>a</code> tag.</p>
<pre><code>WebElement link = driver.findElement(By.tagName("a"));
link.click();</code></pre>
<p>This finds the first anchor element on the page.</p>
<hr>
<h2>14. Tag Name with findElements()</h2>
<p><code>findElements()</code> is particularly useful with Tag Name Locator because a web page can contain many elements having the same tag.</p>
<pre><code>List<WebElement> links = driver.findElements(By.tagName("a"));
System.out.println("Total links: " + links.size());</code></pre>
<p>The above code collects all anchor elements on the page.</p>
<hr>
<h2>15. Find All Input Fields</h2>
<pre><code>List<WebElement> inputs = driver.findElements(By.tagName("input"));
System.out.println("Total input fields: " + inputs.size());</code></pre>
<p>This is useful when testing forms containing multiple input controls.</p>
<hr>
<h2>16. Find All Buttons</h2>
<pre><code>List<WebElement> buttons = driver.findElements(By.tagName("button"));
System.out.println("Total buttons: " + buttons.size());</code></pre>
<hr>
<h2>17. Find All Links</h2>
<pre><code>List<WebElement> links = driver.findElements(By.tagName("a"));
for (WebElement link : links) {
System.out.println(link.getText());
}</code></pre>
<p>This retrieves the visible text of every anchor element.</p>
<hr>
<h2>18. Find All Headings</h2>
<p>You can retrieve all elements having the same heading tag.</p>
<pre><code>List<WebElement> headings = driver.findElements(By.tagName("h2"));
for (WebElement heading : headings) {
System.out.println(heading.getText());
}</code></pre>
<hr>
<h2>19. Find All Images</h2>
<p>Images commonly use the <code>img</code> tag.</p>
<pre><code>List<WebElement> images = driver.findElements(By.tagName("img"));
System.out.println("Total images: " + images.size());</code></pre>
<hr>
<h2>20. Find All Paragraphs</h2>
<pre><code>List<WebElement> paragraphs = driver.findElements(By.tagName("p"));
for (WebElement paragraph : paragraphs) {
System.out.println(paragraph.getText());
}</code></pre>
<hr>
<h2>21. Tag Name vs ID Locator</h2>
<table border="1" cellpadding="8" cellspacing="0">
<tr><th>Feature</th><th>Tag Name</th><th>ID</th></tr>
<tr><td>Strategy</td><td>Uses HTML tag</td><td>Uses id attribute</td></tr>
<tr><td>Syntax</td><td>By.tagName("input")</td><td>By.id("username")</td></tr>
<tr><td>Uniqueness</td><td>Often not unique</td><td>Usually intended to be unique</td></tr>
<tr><td>Multiple elements</td><td>Very useful</td><td>Usually targets one element</td></tr>
<tr><td>Common use</td><td>Collections of elements</td><td>Specific element</td></tr>
</table>
<hr>
<h2>22. Tag Name vs Name Locator</h2>
<table border="1" cellpadding="8" cellspacing="0">
<tr><th>Feature</th><th>Tag Name</th><th>Name</th></tr>
<tr><td>Uses</td><td>HTML tag</td><td>name attribute</td></tr>
<tr><td>Example</td><td>By.tagName("input")</td><td>By.name("username")</td></tr>
<tr><td>Uniqueness</td><td>Usually low</td><td>Depends on HTML</td></tr>
<tr><td>Multiple matches</td><td>Common</td><td>Possible</td></tr>
</table>
<hr>
<h2>23. Tag Name vs Class Name</h2>
<table border="1" cellpadding="8" cellspacing="0">
<tr><th>Feature</th><th>Tag Name</th><th>Class Name</th></tr>
<tr><td>Uses</td><td>HTML tag</td><td>class attribute</td></tr>
<tr><td>Example</td><td>By.tagName("button")</td><td>By.className("login-btn")</td></tr>
<tr><td>Typical usage</td><td>Element type</td><td>Styling/group-based identification</td></tr>
</table>
<hr>
<h2>24. Tag Name vs CSS Selector</h2>
<p>CSS Selector can also select elements by tag name.</p>
<pre><code>driver.findElement(By.cssSelector("input"));</code></pre>
<p>This is conceptually similar to:</p>
<pre><code>driver.findElement(By.tagName("input"));</code></pre>
<p>CSS selectors become more useful when additional attributes or relationships are required.</p>
<hr>
<h2>25. Tag Name vs XPath</h2>
<p>XPath can locate elements using their tag names as well.</p>
<pre><code>driver.findElement(By.xpath("//input"));</code></pre>
<p>The equivalent Tag Name locator is:</p>
<pre><code>driver.findElement(By.tagName("input"));</code></pre>
<p>XPath provides significantly more expression capabilities when the element needs to be located using attributes, text, relationships, or DOM hierarchy.</p>
<hr>
<h2>26. Tag Name for Multiple Elements</h2>
<p>The most practical use of Tag Name Locator is retrieving multiple elements with <code>findElements()</code>.</p>
<pre><code>List<WebElement> elements = driver.findElements(By.tagName("input"));
for (WebElement element : elements) {
System.out.println(element.getAttribute("type"));
}</code></pre>
<hr>
<h2>27. Counting Elements Using Tag Name</h2>
<pre><code>int count = driver.findElements(By.tagName("input")).size();
System.out.println("Input count: " + count);</code></pre>
<p>This can be used in validation and test automation.</p>
<hr>
<h2>28. Verify That a Page Contains Buttons</h2>
<pre><code>List<WebElement> buttons = driver.findElements(By.tagName("button"));
if (buttons.size() > 0) {
System.out.println("Buttons are available.");
} else {
System.out.println("No buttons found.");
}</code></pre>
<hr>
<h2>29. Iterate Through All Links</h2>
<pre><code>List<WebElement> links = driver.findElements(By.tagName("a"));
for (WebElement link : links) {
System.out.println("Text: " + link.getText());
System.out.println("URL: " + link.getAttribute("href"));
}</code></pre>
<hr>
<h2>30. Verify Link URLs</h2>
<pre><code>List<WebElement> links = driver.findElements(By.tagName("a"));
for (WebElement link : links) {
String url = link.getAttribute("href");
System.out.println(url);
}</code></pre>
<p>This can be useful for link validation scenarios.</p>
<hr>
<h2>31. Tag Name with getText()</h2>
<pre><code>WebElement element = driver.findElement(By.tagName("h1"));
String text = element.getText();
System.out.println(text);</code></pre>
<hr>
<h2>32. Tag Name with getAttribute()</h2>
<pre><code>WebElement input = driver.findElement(By.tagName("input"));
String type = input.getAttribute("type");
System.out.println("Input type: " + type);</code></pre>
<hr>
<h2>33. Tag Name with isDisplayed()</h2>
<pre><code>WebElement button = driver.findElement(By.tagName("button"));
if (button.isDisplayed()) {
System.out.println("Button is displayed.");
}</code></pre>
<hr>
<h2>34. Tag Name with isEnabled()</h2>
<pre><code>WebElement button = driver.findElement(By.tagName("button"));
if (button.isEnabled()) {
System.out.println("Button is enabled.");
}</code></pre>
<hr>
<h2>35. Tag Name with isSelected()</h2>
<p><code>isSelected()</code> is generally useful for selectable elements such as checkboxes and radio buttons.</p>
<pre><code>WebElement radio = driver.findElement(By.tagName("input"));
if (radio.isSelected()) {
System.out.println("Element is selected.");
}</code></pre>
<p>When a page contains several input elements, Tag Name alone may locate the wrong input, so a more specific locator should normally be used.</p>
<hr>
<h2>36. Tag Name for Checkboxes</h2>
<p>Checkboxes commonly use the <code>input</code> tag.</p>
<pre><code>List<WebElement> inputs = driver.findElements(By.tagName("input"));
for (WebElement input : inputs) {
if ("checkbox".equals(input.getAttribute("type"))) {
System.out.println("Checkbox found.");
}
}</code></pre>
<hr>
<h2>37. Tag Name for Radio Buttons</h2>
<pre><code>List<WebElement> inputs = driver.findElements(By.tagName("input"));
for (WebElement input : inputs) {
if ("radio".equals(input.getAttribute("type"))) {
System.out.println("Radio button found.");
}
}</code></pre>
<hr>
<h2>38. Tag Name for Text Fields</h2>
<pre><code>List<WebElement> inputs = driver.findElements(By.tagName("input"));
for (WebElement input : inputs) {
if ("text".equals(input.getAttribute("type"))) {
System.out.println("Text field found.");
}
}</code></pre>
<hr>
<h2>39. Tag Name for Password Fields</h2>
<pre><code>List<WebElement> inputs = driver.findElements(By.tagName("input"));
for (WebElement input : inputs) {
if ("password".equals(input.getAttribute("type"))) {
System.out.println("Password field found.");
}
}</code></pre>
<hr>
<h2>40. Tag Name for Submit Buttons</h2>
<p>Some forms use <code>input</code> elements as submit controls.</p>
<pre><code>List<WebElement> inputs = driver.findElements(By.tagName("input"));
for (WebElement input : inputs) {
if ("submit".equals(input.getAttribute("type"))) {
input.click();
}
}</code></pre>
<hr>
<h2>41. Tag Name for Select Elements</h2>
<p>The HTML <code>select</code> tag represents a standard dropdown.</p>
<pre><code>WebElement dropdown = driver.findElement(By.tagName("select"));</code></pre>
<p>For selecting options, Selenium's <code>Select</code> class can be used.</p>
<pre><code>import org.openqa.selenium.support.ui.Select;
WebElement dropdown = driver.findElement(By.tagName("select"));
Select select = new Select(dropdown);
select.selectByVisibleText("India");</code></pre>
<hr>
<h2>42. Tag Name for Option Elements</h2>
<p>Dropdown options are commonly represented using the <code>option</code> tag.</p>
<pre><code>List<WebElement> options = driver.findElements(By.tagName("option"));
for (WebElement option : options) {
System.out.println(option.getText());
}</code></pre>
<hr>
<h2>43. Tag Name for Table Rows</h2>
<p>HTML tables commonly use the <code>tr</code> tag for rows.</p>
<pre><code>List<WebElement> rows = driver.findElements(By.tagName("tr"));
System.out.println("Total rows: " + rows.size());</code></pre>
<hr>
<h2>44. Tag Name for Table Cells</h2>
<p>Table cells normally use the <code>td</code> tag.</p>
<pre><code>List<WebElement> cells = driver.findElements(By.tagName("td"));
for (WebElement cell : cells) {
System.out.println(cell.getText());
}</code></pre>
<hr>
<h2>45. Tag Name for Table Headers</h2>
<p>Table header cells use the <code>th</code> tag.</p>
<pre><code>List<WebElement> headers = driver.findElements(By.tagName("th"));
for (WebElement header : headers) {
System.out.println(header.getText());
}</code></pre>
<hr>
<h2>46. Tag Name for List Items</h2>
<p>HTML unordered and ordered lists commonly contain <code>li</code> elements.</p>
<pre><code>List<WebElement> items = driver.findElements(By.tagName("li"));
for (WebElement item : items) {
System.out.println(item.getText());
}</code></pre>
<hr>
<h2>47. Tag Name for Navigation Menus</h2>
<p>Navigation menus often contain multiple anchor elements. Tag Name can retrieve all links.</p>
<pre><code>List<WebElement> menuLinks = driver.findElements(By.tagName("a"));
for (WebElement link : menuLinks) {
System.out.println(link.getText());
}</code></pre>
<hr>
<h2>48. Tag Name for Images</h2>
<pre><code>List<WebElement> images = driver.findElements(By.tagName("img"));
for (WebElement image : images) {
System.out.println(image.getAttribute("src"));
}</code></pre>
<hr>
<h2>49. Tag Name and Dynamic Web Pages</h2>
<p>Tag Name can work with dynamically generated pages, but the tag itself must remain suitable for identifying the desired element. If many elements share the same tag, additional filtering may be required.</p>
<pre><code>List<WebElement> buttons = driver.findElements(By.tagName("button"));
for (WebElement button : buttons) {
System.out.println(button.getText());
}</code></pre>
<hr>
<h2>50. Tag Name with Filtering</h2>
<p>When Tag Name returns multiple elements, you can inspect attributes and text to identify the required element.</p>
<pre><code>List<WebElement> buttons = driver.findElements(By.tagName("button"));
for (WebElement button : buttons) {
if ("Login".equals(button.getText())) {
button.click();
break;
}
}</code></pre>
<hr>
<h2>51. Tag Name with Index</h2>
<p>When multiple elements are returned, you can access an element by its list index.</p>
<pre><code>List<WebElement> buttons = driver.findElements(By.tagName("button"));
WebElement secondButton = buttons.get(1);
secondButton.click();</code></pre>
<p>Index-based selection should be used carefully because changes in page structure can change the order of elements.</p>
<hr>
<h2>52. Difference Between findElement() and findElements()</h2>
<table border="1" cellpadding="8" cellspacing="0">
<tr><th>Feature</th><th>findElement()</th><th>findElements()</th></tr>
<tr><td>Return type</td><td>WebElement</td><td>List<WebElement></td></tr>
<tr><td>Multiple matches</td><td>Returns first matching element</td><td>Returns all matching elements</td></tr>
<tr><td>No match</td><td>Throws NoSuchElementException</td><td>Returns empty list</td></tr>
<tr><td>Typical use</td><td>Specific element</td><td>Collection of elements</td></tr>
</table>
<hr>
<h2>53. Common Problem with Tag Name Locator</h2>
<p>The main limitation of Tag Name Locator is that HTML tags are frequently reused throughout a page.</p>
<pre><code>driver.findElement(By.tagName("input"));</code></pre>
<p>If a page has ten input elements, Selenium may return the first input rather than the input you actually need.</p>
<hr>
<h2>54. Better Approach for Multiple Similar Elements</h2>
<p>When many elements share the same tag, use a more specific locator.</p>
<pre><code>driver.findElement(By.id("username"));</code></pre>
<p>Or:</p>
<pre><code>driver.findElement(By.name("username"));</code></pre>
<p>Or:</p>
<pre><code>driver.findElement(By.cssSelector("input[name='username']"));</code></pre>
<hr>
<h2>55. Tag Name with CSS Selector for Better Precision</h2>
<p>CSS Selector can combine the tag name with attributes.</p>
<pre><code>driver.findElement(By.cssSelector("input[type='email']"));</code></pre>
<p>This is more specific than:</p>
<pre><code>driver.findElement(By.tagName("input"));</code></pre>
<hr>
<h2>56. Tag Name with XPath for Better Precision</h2>
<pre><code>driver.findElement(By.xpath("//input[@type='email']"));</code></pre>
<p>XPath allows the tag name to be combined with attributes, text, and DOM relationships.</p>
<hr>
<h2>57. Tag Name in a Login Form</h2>
<pre><code>List<WebElement> inputs = driver.findElements(By.tagName("input"));
for (WebElement input : inputs) {
String type = input.getAttribute("type");
if ("text".equals(type)) {
input.sendKeys("admin");
}
if ("password".equals(type)) {
input.sendKeys("password123");
}
}</code></pre>
<hr>
<h2>58. Tag Name in a Registration Form</h2>
<pre><code>List<WebElement> inputs = driver.findElements(By.tagName("input"));
for (WebElement input : inputs) {
String type = input.getAttribute("type");
System.out.println("Input type: " + type);
}</code></pre>
<hr>
<h2>59. Tag Name in an E-Commerce Page</h2>
<p>Tag Name can be useful for retrieving product links, buttons, images, and other groups of elements.</p>
<pre><code>List<WebElement> productLinks = driver.findElements(By.tagName("a"));
for (WebElement link : productLinks) {
System.out.println(link.getText());
}</code></pre>
<hr>
<h2>60. Tag Name in a Product Table</h2>
<pre><code>List<WebElement> rows = driver.findElements(By.tagName("tr"));
for (WebElement row : rows) {
System.out.println(row.getText());
}</code></pre>
<hr>
<h2>61. Tag Name in a Navigation Bar</h2>
<pre><code>List<WebElement> links = driver.findElements(By.tagName("a"));
for (WebElement link : links) {
if (link.isDisplayed()) {
System.out.println(link.getText());
}
}</code></pre>
<hr>
<h2>62. Tag Name in a Form Validation</h2>
<p>You can count input fields and verify that the form contains the expected number of controls.</p>
<pre><code>List<WebElement> inputs = driver.findElements(By.tagName("input"));
int actualCount = inputs.size();
System.out.println("Input count: " + actualCount);</code></pre>
<hr>
<h2>63. Explicit Wait with Tag Name</h2>
<p>For dynamic applications, an explicit wait can be combined with a Tag Name locator.</p>
<pre><code>import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement button = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.tagName("button"))
);
button.click();</code></pre>
<hr>
<h2>64. Wait for a Button to Become Clickable</h2>
<pre><code>WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement button = wait.until(
ExpectedConditions.elementToBeClickable(By.tagName("button"))
);
button.click();</code></pre>
<hr>
<h2>65. Common Exceptions with Tag Name</h2>
<table border="1" cellpadding="8" cellspacing="0">
<tr><th>Exception</th><th>Possible Cause</th></tr>
<tr><td>NoSuchElementException</td><td>No matching element exists</td></tr>
<tr><td>ElementNotInteractableException</td><td>Element exists but cannot currently be interacted with</td></tr>
<tr><td>ElementClickInterceptedException</td><td>Another element blocks the click</td></tr>
<tr><td>StaleElementReferenceException</td><td>Element was replaced or DOM changed</td></tr>
<tr><td>TimeoutException</td><td>Explicit wait condition was not satisfied</td></tr>
</table>
<hr>
<h2>66. Handling NoSuchElementException</h2>
<pre><code>try {
WebElement button = driver.findElement(By.tagName("button"));
button.click();
} catch (org.openqa.selenium.NoSuchElementException e) {
System.out.println("Button was not found.");
}</code></pre>
<hr>
<h2>67. Tag Name and Frames</h2>
<p>If the required element exists inside an iframe, Selenium must first switch to that iframe before locating the element.</p>
<pre><code>WebElement frame = driver.findElement(By.tagName("iframe"));
driver.switchTo().frame(frame);
WebElement input = driver.findElement(By.tagName("input"));
input.sendKeys("Hello");
driver.switchTo().defaultContent();</code></pre>
<hr>
<h2>68. Tag Name and Nested Elements</h2>
<p>When elements are nested, Tag Name can be used to retrieve elements inside a parent element.</p>
<pre><code>WebElement form = driver.findElement(By.tagName("form"));
List<WebElement> inputs = form.findElements(By.tagName("input"));
System.out.println("Inputs inside form: " + inputs.size());</code></pre>
<hr>
<h2>69. Searching Within a Parent Element</h2>
<p>Searching within a parent WebElement can make Tag Name Locator more precise.</p>
<pre><code>WebElement loginForm = driver.findElement(By.tagName("form"));
WebElement button = loginForm.findElement(By.tagName("button"));
button.click();</code></pre>
<hr>
<h2>70. Tag Name with Multiple Forms</h2>
<pre><code>List<WebElement> forms = driver.findElements(By.tagName("form"));
System.out.println("Forms found: " + forms.size());
for (WebElement form : forms) {
List<WebElement> inputs = form.findElements(By.tagName("input"));
System.out.println("Inputs in form: " + inputs.size());
}</code></pre>
<hr>
<h2>71. Tag Name and HTML Structure</h2>
<p>Understanding HTML structure is important when using Tag Name Locator. The locator does not identify an element based on its text, id, class, or custom attributes. It identifies the element based on its tag.</p>
<pre><code><button id="loginButton" class="primary">Login</button></code></pre>
<p>The Tag Name for this element is:</p>
<pre><code>button</code></pre>
<p>The other information such as <code>id</code>, <code>class</code>, and visible text are separate properties.</p>
<hr>
<h2>72. Tag Name with HTML5 Elements</h2>
<p>Tag Name Locator can be used with semantic HTML5 elements such as <code>header</code>, <code>nav</code>, <code>main</code>, <code>section</code>, <code>article</code>, <code>aside</code>, and <code>footer</code>.</p>
<pre><code>WebElement header = driver.findElement(By.tagName("header"));
WebElement nav = driver.findElement(By.tagName("nav"));
WebElement main = driver.findElement(By.tagName("main"));</code></pre>
<hr>
<h2>73. Tag Name for Semantic Elements</h2>
<table border="1" cellpadding="8" cellspacing="0">
<tr><th>Tag</th><th>Typical Purpose</th></tr>
<tr><td>header</td><td>Page or section header</td></tr>
<tr><td>nav</td><td>Navigation</td></tr>
<tr><td>main</td><td>Main content</td></tr>
<tr><td>section</td><td>Content section</td></tr>
<tr><td>article</td><td>Independent content</td></tr>
<tr><td>aside</td><td>Secondary content</td></tr>
<tr><td>footer</td><td>Footer content</td></tr>
</table>
<hr>
<h2>74. Tag Name and Case Sensitivity</h2>
<p>HTML tag names are generally handled case-insensitively in HTML documents, but Selenium locator strings should normally be written using the standard lowercase HTML tag representation.</p>
<pre><code>driver.findElement(By.tagName("input"));</code></pre>
<hr>
<h2>75. Tag Name and Custom Web Components</h2>
<p>Modern web applications may contain custom elements such as:</p>
<pre><code><user-profile></user-profile>
<app-header></app-header></code></pre>
<p>The custom element tag name can be used as a locator where appropriate.</p>
<pre><code>driver.findElement(By.tagName("user-profile"));</code></pre>
<hr>
<h2>76. Tag Name with Shadow DOM</h2>
<p>For elements inside a Shadow DOM, simply using a page-level Tag Name Locator may not be sufficient. Selenium provides Shadow DOM support through shadow root APIs, and the automation script should first access the shadow root before locating elements inside it.</p>
<hr>
<h2>77. Tag Name and Dynamic Content</h2>
<p>Dynamic applications can add or remove elements after page loading. Tag Name can still be used, but synchronization may be necessary.</p>
<pre><code>WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement button = wait.until(
ExpectedConditions.presenceOfElementLocated(By.tagName("button"))
);</code></pre>
<hr>
<h2>78. Tag Name and AJAX Applications</h2>
<p>AJAX-based applications may update the DOM without a complete page refresh. A wait condition can help Selenium locate elements after they become available.</p>
<pre><code>WebElement element = new WebDriverWait(driver, Duration.ofSeconds(10))
.until(ExpectedConditions.visibilityOfElementLocated(By.tagName("div")));</code></pre>
<hr>
<h2>79. Tag Name in Page Object Model</h2>
<p>Tag Name Locator can be used inside a Page Object Model class when the element type is stable and sufficiently specific.</p>
<pre><code>import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class HomePage {
private WebDriver driver;
private By heading = By.tagName("h1");
public HomePage(WebDriver driver) {
this.driver = driver;
}
public String getHeading() {
return driver.findElement(heading).getText();
}
}</code></pre>
<hr>
<h2>80. Reusable Method for Counting Elements</h2>
<pre><code>public int countElements(By locator) {
return driver.findElements(locator).size();
}</code></pre>
<p>Example:</p>
<pre><code>int buttonCount = countElements(By.tagName("button"));
System.out.println("Buttons: " + buttonCount);</code></pre>
<hr>
<h2>81. Reusable Method for Printing Element Text</h2>
<pre><code>public void printElementTexts(By locator) {
List<WebElement> elements = driver.findElements(locator);
for (WebElement element : elements) {
System.out.println(element.getText());
}
}</code></pre>
<hr>
<h2>82. TestNG Example Using Tag Name</h2>
<pre><code>import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class TagNameTest {
WebDriver driver;
@BeforeMethod
public void setUp() {
driver = new ChromeDriver();
driver.get("https://example.com");
}
@Test
public void verifyHeading() {
String heading = driver.findElement(By.tagName("h1")).getText();
Assert.assertFalse(heading.isEmpty());
}
@AfterMethod
public void tearDown() {
driver.quit();
}
}</code></pre>
<hr>
<h2>83. Real-World Example: Validate Page Headings</h2>
<pre><code>List<WebElement> headings = driver.findElements(By.tagName("h2"));
Assert.assertTrue(headings.size() > 0, "No H2 headings found.");
for (WebElement heading : headings) {
System.out.println(heading.getText());
}</code></pre>
<hr>
<h2>84. Real-World Example: Validate Navigation Links</h2>
<pre><code>List<WebElement> links = driver.findElements(By.tagName("a"));
Assert.assertTrue(links.size() > 0, "No links found.");
for (WebElement link : links) {
System.out.println(link.getText());
}</code></pre>
<hr>
<h2>85. Real-World Example: Count Form Controls</h2>
<pre><code>List<WebElement> controls = driver.findElements(By.tagName("input"));
System.out.println("Total form controls: " + controls.size());</code></pre>
<hr>
<h2>86. Real-World Example: Validate Buttons</h2>
<pre><code>List<WebElement> buttons = driver.findElements(By.tagName("button"));
for (WebElement button : buttons) {
System.out.println("Button: " + button.getText());
System.out.println("Displayed: " + button.isDisplayed());
System.out.println("Enabled: " + button.isEnabled());
}</code></pre>
<hr>
<h2>87. Common Mistake: Assuming Tag Name Is Unique</h2>
<p>A common beginner mistake is assuming that a tag name identifies one specific element.</p>
<pre><code>driver.findElement(By.tagName("input"));</code></pre>
<p>A page may contain many input elements, so this locator may not select the intended field.</p>
<hr>
<h2>88. Common Mistake: Using Tag Name for Every Element</h2>
<p>Tag Name should not automatically be the first choice for every element. If an element has a reliable unique ID, Name, CSS selector, or another stable attribute, a more specific locator may be appropriate.</p>
<hr>
<h2>89. Common Mistake: Using Index Without Understanding DOM Order</h2>
<pre><code>List<WebElement> buttons = driver.findElements(By.tagName("button"));
buttons.get(2).click();</code></pre>
<p>This depends on the third button remaining in the same position. If the page structure changes, the test may interact with another button.</p>
<hr>
<h2>90. Common Mistake: Ignoring Dynamic Content</h2>
<p>Immediately locating a dynamically generated element may cause synchronization problems. Use appropriate waits when the application requires them.</p>
<hr>
<h2>91. Best Practices for Tag Name Locator</h2>
<ul>
<li>Use Tag Name when the element type itself is useful.</li>
<li>Use <code>findElements()</code> when working with multiple elements.</li>
<li>Use parent-child searching to narrow the scope when appropriate.</li>
<li>Use stable attributes when Tag Name alone is ambiguous.</li>
<li>Avoid unnecessary index-based selection.</li>
<li>Use explicit waits for dynamic elements when required.</li>
<li>Keep locators readable and maintainable.</li>
<li>Prefer reliable locator strategies over fragile DOM assumptions.</li>
</ul>
<hr>
<h2>92. When Should You Use Tag Name?</h2>
<table border="1" cellpadding="8" cellspacing="0">
<tr><th>Scenario</th><th>Tag Name Suitability</th></tr>
<tr><td>Count all links</td><td>Useful</td></tr>
<tr><td>Count all buttons</td><td>Useful</td></tr>
<tr><td>Find all input fields</td><td>Useful</td></tr>
<tr><td>Read all table rows</td><td>Useful</td></tr>
<tr><td>Read all list items</td><td>Useful</td></tr>
<tr><td>Locate a unique element</td><td>Can be useful</td></tr>
<tr><td>Locate one input among many inputs</td><td>Usually too broad</td></tr>
<tr><td>Locate element by text</td><td>Use another locator</td></tr>
</table>
<hr>
<h2>93. Tag Name Locator Advantages</h2>
<ul>
<li>Simple syntax.</li>
<li>Easy for beginners to understand.</li>
<li>Useful for collections of elements.</li>
<li>Useful for counting elements.</li>
<li>Useful for validating page structure.</li>
<li>Works with standard HTML elements.</li>
<li>Can be combined with WebElement methods.</li>
</ul>
<hr>
<h2>94. Tag Name Locator Limitations</h2>
<ul>
<li>Many elements may share the same tag.</li>
<li>It may not uniquely identify a specific element.</li>
<li>It provides less precision than CSS Selector or XPath.</li>
<li>Index-based selection can be fragile.</li>
<li>Dynamic pages may require additional synchronization.</li>
</ul>
<hr>
<h2>95. Tag Name Locator Interview Question</h2>
<p><strong>Q: What is Tag Name Locator in Selenium?</strong></p>
<p>Tag Name Locator is a Selenium locator strategy that identifies web elements using their HTML tag name.</p>
<hr>
<h2>96. Interview Question: Syntax</h2>
<p><strong>Q: What is the syntax for Tag Name Locator?</strong></p>
<pre><code>driver.findElement(By.tagName("input"));</code></pre>
<hr>
<h2>97. Interview Question: Multiple Elements</h2>
<p><strong>Q: How do you find all elements with the same tag?</strong></p>
<pre><code>List<WebElement> elements =
driver.findElements(By.tagName("input"));</code></pre>
<hr>
<h2>98. Interview Question: findElement vs findElements</h2>
<p><strong>Q: Why is findElements() commonly used with Tag Name?</strong></p>
<p>Because many HTML elements often share the same tag name. For example, a page can contain multiple links, buttons, input fields, rows, or list items.</p>
<hr>
<h2>99. Interview Question: Locate All Links</h2>
<p><strong>Q: How can you locate all links on a page?</strong></p>
<pre><code>List<WebElement> links =
driver.findElements(By.tagName("a"));</code></pre>
<hr>
<h2>100. Interview Question: Locate All Images</h2>
<pre><code>List<WebElement> images =
driver.findElements(By.tagName("img"));</code></pre>
<hr>
<h2>101. Interview Question: Locate Table Rows</h2>
<pre><code>List<WebElement> rows =
driver.findElements(By.tagName("tr"));</code></pre>
<hr>
<h2>102. Interview Question: Tag Name vs XPath</h2>
<p><strong>Q: Can XPath perform the same basic tag selection?</strong></p>
<p>Yes. For example:</p>
<pre><code>By.tagName("input")</code></pre>
<p>can be conceptually represented using:</p>
<pre><code>By.xpath("//input")</code></pre>
<p>XPath can then extend the expression with attributes, text, and DOM relationships.</p>
<hr>
<h2>103. Interview Question: Tag Name vs CSS</h2>
<p><strong>Q: Can CSS Selector select elements by tag name?</strong></p>
<p>Yes.</p>
<pre><code>By.cssSelector("input")</code></pre>
<p>CSS selectors can additionally combine tag names with classes, IDs, attributes, and other CSS selector capabilities.</p>
<hr>
<h2>104. Interview Question: First Matching Element</h2>
<p><strong>Q: What happens when findElement() finds multiple elements?</strong></p>
<p><code>findElement()</code> returns the first matching element found by the locator strategy.</p>
<hr>
<h2>105. Interview Question: No Matching Element</h2>
<p><strong>Q: What happens when findElement() cannot find a matching element?</strong></p>
<p>Selenium throws a <code>NoSuchElementException</code>.</p>
<hr>
<h2>106. Interview Question: No Matching Elements with findElements()</h2>
<p><strong>Q: What does findElements() return when there are no matches?</strong></p>
<p>It returns an empty list rather than throwing <code>NoSuchElementException</code> for the absence of matching elements.</p>
<hr>
<h2>107. Practical Exercise 1: Count All Links</h2>
<pre><code>List<WebElement> links = driver.findElements(By.tagName("a"));
System.out.println("Total links: " + links.size());</code></pre>
<hr>
<h2>108. Practical Exercise 2: Count All Buttons</h2>
<pre><code>List<WebElement> buttons = driver.findElements(By.tagName("button"));
System.out.println("Total buttons: " + buttons.size());</code></pre>
<hr>
<h2>109. Practical Exercise 3: Print All Headings</h2>
<pre><code>List<WebElement> headings = driver.findElements(By.tagName("h2"));
for (WebElement heading : headings) {
System.out.println(heading.getText());
}</code></pre>
<hr>
<h2>110. Practical Exercise 4: Print All List Items</h2>
<pre><code>List<WebElement> items = driver.findElements(By.tagName("li"));
for (WebElement item : items) {
System.out.println(item.getText());
}</code></pre>
<hr>
<h2>111. Practical Exercise 5: Find All Form Inputs</h2>
<pre><code>List<WebElement> inputs = driver.findElements(By.tagName("input"));
for (WebElement input : inputs) {
System.out.println(
input.getAttribute("type")
);
}</code></pre>
<hr>
<h2>112. Practical Exercise 6: Find All Buttons and Their Text</h2>
<pre><code>List<WebElement> buttons = driver.findElements(By.tagName("button"));
for (WebElement button : buttons) {
System.out.println(button.getText());
}</code></pre>
<hr>
<h2>113. Practical Exercise 7: Validate Page Structure</h2>
<pre><code>Assert.assertTrue(
driver.findElements(By.tagName("header")).size() > 0
);
Assert.assertTrue(
driver.findElements(By.tagName("main")).size() > 0
);
Assert.assertTrue(
driver.findElements(By.tagName("footer")).size() > 0
);</code></pre>
<hr>
<h2>114. Practical Project: Web Page Element Analyzer</h2>
<p>A useful beginner Selenium project is a Web Page Element Analyzer that counts and displays different HTML elements on a page.</p>
<pre><code>System.out.println("Links: " +
driver.findElements(By.tagName("a")).size());
System.out.println("Images: " +
driver.findElements(By.tagName("img")).size());
System.out.println("Inputs: " +
driver.findElements(By.tagName("input")).size());
System.out.println("Buttons: " +
driver.findElements(By.tagName("button")).size());
System.out.println("Paragraphs: " +
driver.findElements(By.tagName("p")).size());
System.out.println("Headings: " +
driver.findElements(By.tagName("h1")).size());</code></pre>
<hr>
<h2>115. Practical Project: Form Analyzer</h2>
<pre><code>List<WebElement> inputs = driver.findElements(By.tagName("input"));
for (WebElement input : inputs) {
String type = input.getAttribute("type");
String name = input.getAttribute("name");
System.out.println(
"Type: " + type + ", Name: " + name
);
}</code></pre>
<hr>
<h2>116. Practical Project: Link Analyzer</h2>
<pre><code>List<WebElement> links = driver.findElements(By.tagName("a"));
for (WebElement link : links) {
System.out.println("Text: " + link.getText());
System.out.println("URL: " + link.getAttribute("href"));
}</code></pre>
<hr>
<h2>117. Practical Project: Table Analyzer</h2>
<pre><code>List<WebElement> rows = driver.findElements(By.tagName("tr"));
for (WebElement row : rows) {
List<WebElement> cells = row.findElements(By.tagName("td"));
for (WebElement cell : cells) {
System.out.print(cell.getText() + " | ");
}
System.out.println();
}</code></pre>
<hr>
<h2>118. Tag Name Locator Best-Practice Checklist</h2>
<ul>
<li>Understand the HTML structure before selecting the locator.</li>
<li>Use lowercase standard tag names.</li>
<li>Use <code>findElements()</code> for collections.</li>
<li>Use Tag Name when the element type is relevant.</li>
<li>Do not assume the tag name is unique.</li>
<li>Use attributes or another locator when additional precision is required.</li>
<li>Avoid unnecessary list indexes.</li>
<li>Use explicit waits for dynamic elements.</li>
<li>Keep locators maintainable.</li>
<li>Validate the locator in browser developer tools when necessary.</li>
</ul>
<hr>
<h2>119. Quick Reference Table</h2>
<table border="1" cellpadding="8" cellspacing="0">
<tr><th>HTML Element</th><th>Tag Name</th><th>Selenium Example</th></tr>
<tr><td>Text Input</td><td>input</td><td>By.tagName("input")</td></tr>
<tr><td>Button</td><td>button</td><td>By.tagName("button")</td></tr>
<tr><td>Link</td><td>a</td><td>By.tagName("a")</td></tr>
<tr><td>Image</td><td>img</td><td>By.tagName("img")</td></tr>
<tr><td>Heading</td><td>h1</td><td>By.tagName("h1")</td></tr>
<tr><td>Paragraph</td><td>p</td><td>By.tagName("p")</td></tr>
<tr><td>Table Row</td><td>tr</td><td>By.tagName("tr")</td></tr>
<tr><td>Table Cell</td><td>td</td><td>By.tagName("td")</td></tr>
<tr><td>List Item</td><td>li</td><td>By.tagName("li")</td></tr>
<tr><td>Dropdown</td><td>select</td><td>By.tagName("select")</td></tr>
<tr><td>Dropdown Option</td><td>option</td><td>By.tagName("option")</td></tr>
<tr><td>Form</td><td>form</td><td>By.tagName("form")</td></tr>
</table>
<hr>
<h2>120. Tag Name Locator Learning Flow</h2>
<pre><code>Understand HTML
↓
Identify Element Tag
↓
Choose Tag Name
↓
Use By.tagName()
↓
Use findElement() or findElements()
↓
Interact with WebElement
↓
Validate Result
↓
Add Waits if Required
↓
Create Maintainable Automation Test</code></pre>
<hr>
<h2>121. Summary of Tag Name Locator</h2>
<p>Tag Name Locator identifies web elements based on their HTML tag name. It is simple and particularly useful when the automation test needs to retrieve multiple elements of the same type. Common examples include finding all links using <code>a</code>, all input fields using <code>input</code>, all buttons using <code>button</code>, all rows using <code>tr</code>, and all list items using <code>li</code>.</p>
<p>The most important limitation is that tag names are frequently shared by many elements. Therefore, Tag Name should be combined with a more specific locator or additional filtering whenever a particular element must be identified.</p>
<hr>
<h2>122. Final Example</h2>
<pre><code>import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.List;
public class FinalTagNameExample {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
WebElement heading =
driver.findElement(By.tagName("h1"));
System.out.println("Heading: " + heading.getText());
List<WebElement> links =
driver.findElements(By.tagName("a"));
System.out.println("Total Links: " + links.size());
List<WebElement> images =
driver.findElements(By.tagName("img"));
System.out.println("Total Images: " + images.size());
driver.quit();
}
}</code></pre>
<hr>
<h2>123. Key Points to Remember</h2>
<ul>
<li><strong>Tag Name Locator:</strong> Identifies elements using their HTML tag name.</li>
<li><strong>Syntax:</strong> <code>By.tagName("tagName")</code></li>
<li><strong>Single element:</strong> Use <code>findElement()</code>.</li>
<li><strong>Multiple elements:</strong> Use <code>findElements()</code>.</li>
<li><strong>Common tags:</strong> <code>input</code>, <code>button</code>, <code>a</code>, <code>img</code>, <code>p</code>, <code>h1</code>, <code>tr</code>, <code>td</code>, <code>li</code>.</li>
<li><strong>Main advantage:</strong> Simple and useful for collections.</li>
<li><strong>Main limitation:</strong> Tag names are often not unique.</li>
<li><strong>Better precision:</strong> Use ID, Name, CSS Selector, XPath, or another stable locator when necessary.</li>
<li><strong>Dynamic pages:</strong> Use appropriate waits when elements are loaded asynchronously.</li>
</ul>
<hr>
<h2>124. Conclusion</h2>
<p>Tag Name is an important Selenium locator strategy for identifying web elements through their HTML tag names. It is especially useful when working with groups of similar elements such as links, buttons, inputs, table rows, images, headings, and list items.</p>
<p>For beginner Selenium automation, understanding Tag Name helps build a strong foundation in WebElement identification. As automation scenarios become more complex, Tag Name can be combined with parent-element searches, WebElement attributes, CSS Selectors, XPath, and synchronization techniques to create more precise and maintainable tests.</p>
<p>To learn more about Selenium automation testing, visit the <a href="https://www.justacademy.co/course-detail/selenium-training" target="_blank" rel="noopener noreferrer">JustAcademy's Selenium Training Course</a>.</p>
<p>For course demonstration and enrollment-related information, visit <a href="https://www.justacademy.co/register-for-course-demo" target="_blank" rel="noopener noreferrer">Register for Course Demo</a>.</p>
</div>