- 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
- Python編程自學(xué)手冊(cè)
- Machine Learning with R Cookbook(Second Edition)
- Python測(cè)試開(kāi)發(fā)入門與實(shí)踐
- OpenCV for Secret Agents
- TypeScript圖形渲染實(shí)戰(zhàn):基于WebGL的3D架構(gòu)與實(shí)現(xiàn)
- TradeStation交易應(yīng)用實(shí)踐:量化方法構(gòu)建贏家策略(原書第2版)
- 網(wǎng)站構(gòu)建技術(shù)
- 深入理解Elasticsearch(原書第3版)
- Learning Probabilistic Graphical Models in R
- MySQL從入門到精通(軟件開(kāi)發(fā)視頻大講堂)
- SQL Server 2008 R2數(shù)據(jù)庫(kù)技術(shù)及應(yīng)用(第3版)
- JavaScript機(jī)器人編程指南
- 遠(yuǎn)方:兩位持續(xù)創(chuàng)業(yè)者的點(diǎn)滴思考
- Mastering jQuery Mobile
- Data Science Algorithms in a Week