This post was most recently updated on June 14th, 2019
Locator Type: Tag Name
- HTML document is structure of different html elements which normally contains start tag with attribute closing tag and some text within opening and closing tag.
- Opening tag consist within Tag Name within two angular brackets (‘<’ and ‘>’) while closing tag contains forward slash before Tag Name within angular brackets.
Java code to find web element using Tag Name.
For single WebElement
WebElement element=driver.findElement(By.tagName(“tag_Name”));
For multiple WebElement
List<WebElement> elements=driver.findElements(By.tagName(“tag_Name”));
Steps to find web element availability or count for specific tag name.
- Open url “https://www.societyhive.com/” in chrome browser.
- Click F12 to open DevTools in chrome browser.
- Click on elements tab and press shortcut key Ctrl+F.
- Enter tag name to find count on web element with specific tag name.
Single element
WebElement inputElement=driver.findElement(By.tagName("input"));
Multiple element
List<WebElement> allInputElements=driver.findElements(By.tagName("a"));
Sample code to print href attribute value for all anchor element:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; public class SeleniumMain { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "./chromedriver.exe"); WebDriver driver; ChromeOptions cOptions=new ChromeOptions(); cOptions.addArguments("--start-maximized"); driver=new ChromeDriver(cOptions); driver.get("https://www.societyhive.com/"); //print href attribute of all anchor element on home page List<WebElement> linkElements=driver.findElements(By.tagName("a")); for (WebElement webElement : linkElements) { System.out.println(webElement.getAttribute("href"));; } driver.close(); } } |