Careeroza
Tag Name and Link Text
By.tagName
List<WebElement> links = driver.findElements(By.tagName("a"));
System.out.println("Links on home page: " + links.size());
List<WebElement> images = driver.findElements(By.tagName("img"));Example
Output:
Run TagNameTest.main()Example output
Links on home page: 87
tagName is used mostly with findElements to count or loop through all links, images or table rows.
By.linkText and By.partialLinkText
<nav>
<a href="/orders">My Orders</a>
<a href="/offers">Today's Offers (12)</a>
</nav>driver.findElement(By.linkText("My Orders")).click(); // exact visible text
driver.findElement(By.partialLinkText("Today's Offers")).click(); // part of the text; the count can changeNote: These work only on <a> links. The text must match what the user sees, including case.