Java 爪哇。硒网络驱动程序。从表格中获取单元格值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36263315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java. Selenium webdriver. Get cell value from table
提问by Kleeo
Here's a piece of my html
这是我的 html 的一部分
<table class="table_results data ajax" data-uniqueId="20605">
<thead>
</thead>
<tbody>
<tr class="odd">
<td data-decimals="0" data-type="int" class="right data not_null nowrap">1</td>
<td data-decimals="0" data-type="string" data-originallength="5" class="data not_null text ">user1</td>
<td data-decimals="0" data-type="string" data-originallength="40" class="data not_null text ">e38ad214943daad1d64c102faec29de4afe9da3d</td>
<td data-decimals="0" data-type="string" data-originallength="14" class="data not_null text ">[email protected]</td>
<td data-decimals="0" data-type="string" data-originallength="6" class="data not_null text ">Smith</td>
<td data-decimals="0" data-type="string" data-originallength="1" class="data not_null text ">"</td>
</tr>
</tbody>
</table>
So what I need is to retrieve cell values. One more thing is that when viewing code via browser's source code option it looks same as above. But when using Firefox's firebug it additionally shows that values are inside of
所以我需要的是检索单元格值。还有一件事是,当通过浏览器的源代码选项查看代码时,它看起来与上面相同。但是当使用 Firefox 的萤火虫时,它另外显示值在
<span></span>
tag
标签
I know that here xPath is the solution. For the past 2 hours I've tried so many but none of it worked. And of course I've seen similar questions' solutions on SO but somehow they are not suitable for me.
我知道这里 xPath 是解决方案。在过去的 2 个小时里,我尝试了很多,但没有一个奏效。当然,我在 SO 上看到了类似问题的解决方案,但不知何故它们不适合我。
What I'm trying to do:
我正在尝试做的事情:
WebElement table_element = driver.findElement(By.xpath("//table//tbody"));
ArrayList<WebElement> rows = (ArrayList<WebElement>) table_element.findElements(By.tagName("tr"));
for (WebElement row : rows) {
ArrayList<WebElement> cells = (ArrayList<WebElement>) row.findElements(By.tagName("//td"));
for (WebElement cell : cells) {
System.out.println(cell.getText());
}
}
回答by alecxe
Locate the td
elements and call .getText()
method:
定位td
元素并调用.getText()
方法:
List<WebElement> rows = driver.findElements(By.cssSelector("table.table_results tr"));
for (WebElement row: rows) {
List<WebElement> cells = row.findElements(By.cssSelector("td.data"));
for (WebElement cell: cells) {
System.out.println(cell.getText());
}
}
Here, we are using CSS selectorsto locate table rows and then row cells for every table row.
在这里,我们使用CSS 选择器来定位表格行,然后为每个表格行定位行单元格。
回答by Leon Barkan
List<WebElement> TRCollection = driver.findElement(By.className("table_results data ajax")).findElements(By.tagName("tr"));
for (WebElement tr : TRCollection)
{
List<WebElement> TDCollection = tr.findElements(By.tagName("td"));
for (WebElement td: TDCollection)
{
System.out.println(td.getText());
}
}
try to find your table by:
尝试通过以下方式找到您的桌子:
List<WebElement> tables = driver.findElements(By.tagName("table"));
debug it and find your table after that you can do the upper code:
调试它并找到您的表,然后您可以执行上层代码:
for example something like...
例如像...
List<WebElement> TRCollection = tables[2].findElements(By.tagName("tr"));
回答by Striter Alfa
To complement the previous awnser, if you need get the texts from a specific row, you can do this way:
为了补充之前的 awnser,如果您需要从特定行获取文本,您可以这样做:
string userName = "user1";
WebElement row = driver.findElement(By.xPath("//table[contains(@class, 'table_results')]/tbody/tr/td[2][text()='" + userName + "']/.."));
List<WebElement> cells = row.findElements(By.cssSelector("td.data"));
for (WebElement cell: cells) {
System.out.println(cell.getText());
}
In this case, I'm using td[2]
to get the second column of the row and compare the username. You can chage it if desire compare another fields.
在这种情况下,我使用td[2]
获取行的第二列并比较用户名。如果需要比较其他领域,您可以更改它。
Also, you can assert if the xpath selector is working or if it is returning all that you want using the chrome console:
此外,您可以断言 xpath 选择器是否正在工作,或者它是否正在使用 chrome 控制台返回您想要的所有内容:
$x("your xpath selector here")
回答by sideshowmanny
/**
* match a string to the text of a table cell and return that row.
*
* @param table the table the data is in
* @param cellTextEquals the text to identify the row e.g. unique ID
* @param intCellToFind the column index in which to look for that text
* @return table row
*/
protected WebElement getRowFromTable(WebElement table,
String cellTextEquals, int intCellToFind) {
explicitWaitForElementToBeVisibleAndClickable(table);
WebElement tableBody = table.findElement(By.tagName("tbody"));
List<WebElement> rows = tableBody.findElements(By.tagName("tr"));
for (WebElement row : rows) {
List<WebElement> td = row.findElements(By.tagName("td"));
if (td.size() > 0
&& td.get(intCellToFind).getText().equals(cellTextEquals)) {
return row;
}
}
return null;
}
/**
* match a string to the text of a cell and return the text of
* another specified cell in that row
*
* @param table the table the data is in
* @param cellTextEquals the text to identify the row e.g. unique ID
* @param intCellToFind the column index in which to look for that text
* @param intCellToReturn the column index of the cell text you want to return
* @return text of a specific cell in an html table
*/
protected String getTextFromTableCell(WebElement table, String cellTextEquals,
int intCellToFind, int intCellToReturn) {
WebElement row = getRowFromTable(table, cellTextEquals, intCellToFind);
List<WebElement> td = row.findElements(By.tagName("td"));
if (td.get(intCellToFind).getText().equals(cellTextEquals)) {
return td.get(intCellToReturn).getText();
}
return null;
}
/**
* Gets an element from a table, either button or tagName, from a table.
*
* @param table the table the data is in
* @param cellTextEquals the text to identify the row e.g. unique ID
* @param intCellToFind the column index in which to look for that text
* @param intCellToReturn the column index of the cell where the element is you want to return
* @param buttonText the button text if it's a button element you want
* @param tagName the tag name of the element i=icon, a=link etc.
* @return Element By.tagName or button
*/
protected WebElement getElementFromTableCell(WebElement table,
String cellTextEquals, int intCellToFind, int intCellToReturn,
String buttonText, String tagName) {
WebElement row = getRowFromTable(table, cellTextEquals, intCellToFind);
if (row != null) {
List<WebElement> td = row.findElements(By.tagName("td"));
if (td.get(intCellToFind).getText().equals(cellTextEquals)) {
if (!buttonText.equals("")) {
return td.get(intCellToReturn).findElement(By.xpath(
"//button[contains(text(),'" + buttonText + "')]"));
} else {
return td.get(intCellToReturn)
.findElement(By.tagName(tagName));
}
}
}
return null;
}