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

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
主站蜘蛛池模板: 永清县| 河津市| 安丘市| 平度市| 古丈县| 通江县| 武胜县| 新乡市| 象州县| 兰溪市| 伊春市| 西盟| 志丹县| 紫金县| 桂东县| 图片| 永兴县| 隆安县| 长泰县| 洛浦县| 富民县| 天全县| 依兰县| 鹤岗市| 盐山县| 织金县| 泉州市| 高雄县| 西峡县| 屏东市| 怀远县| 股票| 甘德县| 巴彦淖尔市| 思茅市| 商洛市| 新绛县| 阳城县| 昔阳县| 西城区| 九江市|