Selenium Webdriver - 获取表数据 [英] Selenium Webdriver - Fetching Table Data
本文介绍了Selenium Webdriver - 获取表数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从UI中的表中获取数据。我知道使用tr和td循环遍历行和列。但我所拥有的表格是这样的:
I want to fetch data from tables in UI. I know about looping through rows and columns using "tr" and "td". But the one the table I have is something like this:
<table>
<tbody>
<tr><td>data</td><th>data</th><td>data</td><td>data</td></tr>
<tr><td>data</td><th>data</th><td>data</td><td>data</td></tr>
<tr><td>data</td><th>data</th><td>data</td><td>data</td></tr>
</tbody>
</table>
如何使我的代码通用,以便可以处理中间出现的TH 。
目前,我正在使用此代码:
How can I make my code generic, so that the occurrence of "TH" in middle can be handled. Currently, I am using this code :
// Grab the table
WebElement table = driver.findElement(By.id(searchResultsGrid));
// Now get all the TR elements from the table
List<WebElement> allRows = table.findElements(By.tagName("tr"));
// And iterate over them, getting the cells
for (WebElement row : allRows) {
List<WebElement> cells = row.findElements(By.tagName("td"));
for (WebElement cell : cells) {
// And so on
}
}
推荐答案
你可以在不区分td和th的情况下查找tr元素的所有子元素。因此,而不是
You could look for all children of tr element without differentiating between td and th. So instead of
List<WebElement> cells = row.findElements(By.tagName("td"));
我会使用
List<WebElement> cells = row.findElements(By.xpath("./*"));
这篇关于Selenium Webdriver - 获取表数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文