ThegetTagNamemethod can be called from all the WebElements. This will return the HTML tag name of the WebElement. For example, in the following HTML code, the button is the tag name of the HTML element:
In the preceding code, the button is the tag name of the HTML element.
The API syntax for thegetTagName()method is as follows:
java.lang.String getTagName()
The return type of the preceding method is String, and it returns the tag name of the target element.
The following is the code that returns the tag name of the Search button:
@Test public void elementGetTagNameExample() { WebElement searchButton = driver.findElement(By.className("search-button")); System.out.println("Html tag of the button is: " + searchButton.getTagName()); }
The preceding code uses thegetTagName()method to get the tag name of the Search button element. The output of the code is as expected: