【问题标题】:How to get text from each cell of an HTML table?如何从 HTML 表格的每个单元格中获取文本?
【发布时间】:2016-02-27 02:51:36
【问题描述】:
在 Selenium 2.0 中,我不知道如何遍历网页中的 HTML 表格。在selenium2.0 javadoc中,我找到了“TableFinder”和“TableCellFinder”两个类,但是我找不到任何例子。
我想做这样的事情:
RowCount=Get how many rows are there in the html table
for each row of the table
{
column_count=Get column count
for each column
{
cell_value=get_text_from(row,col);
Do something with cell_value
}
}
如何从每个表格单元格中获取文本?
【问题讨论】:
标签:
selenium
selenium-webdriver
【解决方案1】:
感谢之前的回复。
我找到了使用 selenium 2.0 类的解决方案。
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class WebTableExample
{
public static void main(String[] args)
{
WebDriver driver = new InternetExplorerDriver();
driver.get("http://localhost/test/test.html");
WebElement table_element = driver.findElement(By.id("testTable"));
List<WebElement> tr_collection=table_element.findElements(By.xpath("id('testTable')/tbody/tr"));
System.out.println("NUMBER OF ROWS IN THIS TABLE = "+tr_collection.size());
int row_num,col_num;
row_num=1;
for(WebElement trElement : tr_collection)
{
List<WebElement> td_collection=trElement.findElements(By.xpath("td"));
System.out.println("NUMBER OF COLUMNS="+td_collection.size());
col_num=1;
for(WebElement tdElement : td_collection)
{
System.out.println("row # "+row_num+", col # "+col_num+ "text="+tdElement.getText());
col_num++;
}
row_num++;
}
}
}
【解决方案2】:
这是我刚刚编写的 C# 示例,大致基于使用 CSS 选择器的答案,希望对其他人有用,以了解如何设置表行的 ReadOnlyCollection 并至少在 MS 领域对其进行迭代。我正在查看一组表行,以查找带有 OriginatorsRef(只是一个字符串)的行和带有包含标题属性的图像的 TD 的行,其中包含 Overdue by:
public ReadOnlyCollection<IWebElement> GetTableRows()
{
this.iwebElement = GetElement();
return this.iwebElement.FindElements(By.CssSelector("tbody tr"));
}
在我的主要代码中:
...
ReadOnlyCollection<IWebElement> TableRows;
TableRows = f.Grid_Fault.GetTableRows();
foreach (IWebElement row in TableRows)
{
if (row.Text.Contains(CustomTestContext.Current.OriginatorsRef) &&
row.FindElements(By.CssSelector("td img[title*='Overdue by']")).Count > 0)
return true;
}
【解决方案3】:
我没有使用 Selenium 2。Selenium 1.x 有 selenium.getTable("tablename".columnNumber.rowNumber) 来到达所需的单元格。也许您可以使用webdriverbackedselenium 并执行此操作。
您可以通过使用获取总行数和列数
int numOfRows = selenium.getXpathCount("//table[@id='tableid']//tr")
int numOfCols=selenium.getXpathCount("//table[@id='tableid']//tr//td")
【解决方案4】:
$content = '';
for($rowth=0; $rowth<=100; $rowth++){
$content .= $selenium->getTable("tblReports.{$rowth}.0") . "\n";
//$content .= $selenium->getTable("tblReports.{$rowth}.1") . "\n";
$content .= $selenium->getTable("tblReports.{$rowth}.2") . " ";
$content .= $selenium->getTable("tblReports.{$rowth}.3") . " ";
$content .= $selenium->getTable("tblReports.{$rowth}.4") . " ";
$content .= $selenium->getTable("tblReports.{$rowth}.5") . " ";
$content .= $selenium->getTable("tblReports.{$rowth}.6") . "\n";
}
【解决方案5】:
另一个 C# 示例。我只是为它做了一个扩展方法。
public static string GetCellFromTable(this IWebElement table, int rowIndex, int columnIndex)
{
return table.FindElements(By.XPath("./tbody/tr"))[rowIndex].FindElements(By.XPath("./td"))[columnIndex].Text;
}
【解决方案6】:
它的
selenium.getTable("tablename".rowNumber.colNumber)
不是
selenium.getTable("tablename".colNumber.rowNumber)