In Selenium 2.0, I have no idea how to traverse through a HTML table in a webpage. In selenium2.0 javadoc, I found two classes "TableFinder" and "TableCellFinder", but I couldn't find any examples.
I want to do something like this:
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
}
}
How can I get the text from each of the table cells?
This question is related to
selenium
selenium-webdriver
its
selenium.getTable("tablename".rowNumber.colNumber)
not
selenium.getTable("tablename".colNumber.rowNumber)
Another C# example. I just made an extension method for it.
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;
}
$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";
}
I have not used Selenium 2. Selenium 1.x has selenium.getTable("tablename".columnNumber.rowNumber)
to reach the required cell. May be you can use webdriverbackedselenium
and do this.
And you can get the total rows and columns by using
int numOfRows = selenium.getXpathCount("//table[@id='tableid']//tr")
int numOfCols=selenium.getXpathCount("//table[@id='tableid']//tr//td")
Here's a C# example I just cooked up, loosely based on the answer using CSS selectors, hopefully of use to others for seeing how to setup a ReadOnlyCollection
of table rows and iterate over it in MS land at least. I'm looking through a collection of table rows to find a row with an OriginatorsRef
(just a string) and a TD
with an image that contains a title attribute with Overdue by
in it:
public ReadOnlyCollection<IWebElement> GetTableRows()
{
this.iwebElement = GetElement();
return this.iwebElement.FindElements(By.CssSelector("tbody tr"));
}
And within my main code:
...
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;
}
Source: Stackoverflow.com