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

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
主站蜘蛛池模板: 镇原县| 河曲县| 县级市| 丹凤县| 淮安市| 舞钢市| 德保县| 陇南市| 江门市| 祁阳县| 南康市| 广丰县| 利辛县| 安阳市| 巨鹿县| 慈溪市| 宁波市| 佛学| 龙南县| 西畴县| 旺苍县| 论坛| 景东| 德令哈市| 汕头市| 利辛县| 乌拉特中旗| 凤山县| 阳原县| 眉山市| 亳州市| 南安市| 平阳县| 军事| 和田县| 德钦县| 额尔古纳市| 永康市| 佛山市| 梨树县| 德令哈市|