getTagName() method in Selenium Java

The getTagName() method is used to get the tag name of the web element. This post will discuss the getTagName() method of the WebElement interface in detail.

  • Method declaration – String getTagName()
  • What does it do? It returns a string representing the tag name of the web element.

A WebElement represents a website’s HTML element. HTML element contains a start tag and an end tag with the content lying between those tags.

<Tag> content </Tag>

So, we take the example of a link-type element.

<a href = "www.codekru.com"> Codekru </a>

Here, “a” is a tag, and “href” is its attribute. So, if we use getTagName() on this web element, it will return “a” as the tag name.

Code Example

We will try to find the tag name of an input type element. We have highlighted one such element in the below image, and the element can be found on this page if you want to try it yourself.

input text field element
  • We will first find the input type element using the findElement() method. We can use the id attribute to find the element.
    • WebElement element = driver.findElement(By.id(“firstName”))
  • Afterward, we can use the getTagName() method on the element.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class CodekruTest {

	@Test
	public void test() {

		// pass the path of the chromedriver location in the second argument
		System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
		WebDriver driver = new ChromeDriver();

		// opening the url
		driver.get("https://testkru.com/Elements/TextFields");

		WebElement element = driver.findElement(By.id("firstName"));
		System.out.println("Tag Name of the element: " + element.getTagName());

	}
}

Output –

Tag Name of the element: input

And similarly, we can find the tag of any of the web elements.

What if we use the getTagName() method on a null WebElement?

We will get a NullPointerException as illustrated by the below example.

public class CodekruTest {

	@Test
	public void test() {

		// pass the path of the chromedriver location in the second argument
		System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
		WebDriver driver = new ChromeDriver();

		// opening the url
		driver.get("https://testkru.com/Elements/TextFields");

		WebElement element = null;
		System.out.println("Tag Name of the element: " + element.getTagName());

	}
}

Output –

java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.WebElement.getTagName()" because "element" is null

This is it. Please visit this link to learn more about WebElement and its methods.

We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at [email protected]

Liked the article? Share this on

Leave a Comment

Your email address will not be published.