1.

How to get TagName?

Answer»

The tagName property returns the tag name of the element.

A tagName is a part of a DOM structure. Every element on a page is defined with a tag like input tag, button tag, anchor tag, etc. Each tag can have multiple ATTRIBUTES like ID, name, value class, etc. In the case of the tagName locator in Selenium, we are SIMPLY using the tag name to identify an element.

See below example of DOM structure of the login page where tag names are highlighted

- Email Field: < input type="email" name="email" value="" placeholder="Email" required="required" autofocus="autofocus" class="form-control md-5 form-control-lg"> Password Field: < input type="password" name="password" placeholder="Password" class="form-control md-5 form-control-lg" > Login Button: < button type="submit" class="btn btn-primary btn-lg btn-block md-5">LOGIN< /button > Forgot Password Link: < button type="submit" class="btn btn-primary btn-lg btn-block md-5">LOGIN< /button >

When to use this tagName locator in Selenium? where you do not have attribute values like ID, class, or name and you want to locate an element, you can use the tagName locator in Selenium. For example, to retrieve data from a table, you can use < td > tag or < tr > tag.

Similarly, in a scenario where you wish to verify the number of links on a page and verify whether links are WORKING or not, you can locate all such links through the anchor tag.

The COMMAND to identify an element via tagName is:

driver.findElement(By.tagName("input"));

This method is not reliable while locating individual elements and the page might have multiple INSTANCES of these elements.



Discussion

No Comment Found