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.
- 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 admin@codekru.com.
i didn’t understand how to solve this error “java.lang.NullPointerException: Cannot invoke “org.openqa.selenium.WebElement.getTagName()” because “element” is null” can you please help me
If you encounter this error, it may be because the element is null. To resolve this, it would be best to ensure that the element object is not set to null in the code and that the object has been initialized properly.
how to check if element has div tag or not ? for eg. if element has select class and div is not present then how to handle nosuchelementexception?
Hi there! Can you provide more details about your request? It would be helpful if you could share an HTML snippet.