- Selenium Testing Tools Cookbook(Second Edition)
- Unmesh Gundecha
- 183字
- 2021-07-09 21:14:20
Finding elements by tag name
Selenium WebDriver's By
class provides a tagName()
method to find elements by their HTML tag name. This is similar to the getElementsByTagName()
DOM method in JavaScript.
This is used when you want to locate elements using their tag name, for example, locating all <tr>
tags in a table.
In this recipe, we will briefly see how to use the tagName()
locator method.
How to do it...
Let's assume you have a single button element on a page. You can locate this button by using its tag in the following way:
WebElement loginButton = driver.findElement(By.tagName("button")); loginButton.click();
Take another example where we want to count how many rows are displayed in <table>
. We can do this in the following way:
WebElement table = driver.findElement(By.id("summaryTable")); List<WebElement> rows = table.findElements(By.tagName("tr")); assertEquals(10, rows.size());
How it works...
The tagName()
locator method queries the DOM and returns a list of matching elements for the specified tag name. This method may not be reliable while locating individual elements and the page might have multiple instances of these elements.
See also
- The Finding elements using findElements method recipe
- HornetQ Messaging Developer’s Guide
- Vue.js前端開發(fā)基礎與項目實戰(zhàn)
- Java高手真經(jīng)(高級編程卷):Java Web高級開發(fā)技術(shù)
- Mastering QGIS
- Java FX應用開發(fā)教程
- 編寫整潔的Python代碼(第2版)
- Groovy for Domain:specific Languages(Second Edition)
- Quarkus實踐指南:構(gòu)建新一代的Kubernetes原生Java微服務
- Teaching with Google Classroom
- Tableau 10 Bootcamp
- 微服務從小白到專家:Spring Cloud和Kubernetes實戰(zhàn)
- Android項目實戰(zhàn):手機安全衛(wèi)士開發(fā)案例解析
- 深入實踐Kotlin元編程
- Drupal Search Engine Optimization
- 征服C指針(第2版)