官术网_书友最值得收藏!

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
主站蜘蛛池模板: 金乡县| 柘荣县| 泰兴市| 黄骅市| 拉萨市| 东乌珠穆沁旗| 阿拉善盟| 那曲县| 东丽区| 济宁市| 南靖县| 河源市| 永清县| 来宾市| 绵竹市| 梅河口市| 扎囊县| 中西区| 平凉市| 徐闻县| 措勤县| 双流县| 金寨县| 吉林市| 青田县| 敦煌市| 定远县| 广灵县| 乡宁县| 介休市| 元谋县| 海林市| 云安县| 班玛县| 徐州市| 探索| 博客| 襄城县| 和政县| 慈利县| 包头市|