Reputation: 1091
I'm trying to get the text of a table cell based on the cell index and a row identifier. In the sample table below, I'd like to get the text of the cell in the same row as the cell "text6". Here's a sample of the table I'm dealing with:
<table cellspacing="0" cellpadding="0" class="fixedTable" id="PackageTable_dataTable" style="width: 1262px;">
<tbody class="tableData" id="PackageTable_dataBody">
<tr height="24px" uid="section1" index="0" class="alternateRow">
<td><div style="overflow: hidden; width: 193px;"><span title="text1">text1</span></div></td>
<td><div style="overflow: hidden; width: 57px;"><span title="text2">text2</span></div></td>
<td><div style="overflow: hidden; width: 115px;"><span title="text3">text3</span></div></td>
</tr>
<tr height="24px" uid="section2" index="1" class="alternateRow">
<td><div style="overflow: hidden; width: 193px;"><span title="text4">text4</span></div></td>
<td><div style="overflow: hidden; width: 57px;"><span title="text5">text5</span></div></td>
<td><div style="overflow: hidden; width: 115px;"><span title="text6">text6</span></div></td>
</tr>
<tr height="24px" uid="section3" index="2" class="alternateRow">
<td><div style="overflow: hidden; width: 193px;"><span title="text7">text7</span></div></td>
<td><div style="overflow: hidden; width: 57px;"><span title="text8">text8</span></div></td>
<td><div style="overflow: hidden; width: 115px;"><span title="text9">text9</span></div></td>
</tr>
</tbody>
</table>
and here's my non-working code:
public static string returnTableCellValue(string TableID, string TableRowIdentifier, int targetCellIndex)
{
string cellValue = string.Empty;
try
{
IWebElement baseTable = Global.driver.FindElement(By.Id(TableID));
// gets all table rows
ICollection<IWebElement> rows = baseTable.FindElements(By.TagName("tr"));
// for every row
foreach (IWebElement row in rows)
{
if (row.FindElement(By.XPath("//span[text()='" + TableRowIdentifier + "']")).Displayed)
{
Global.log.WriteLine("row identifier found!");
IWebElement key = row.FindElement(By.XPath("//td[" + targetCellIndex + "]"));
IWebElement keySpan = key.FindElement(By.TagName("span"));
cellValue = keySpan.Text;
}
}
return cellValue;
}
catch (Exception ex)
{
Global.log.WriteLine("returnTableCellValue exception: " + ex.ToString());
return string.Empty;
}
}
Any idea how to do this?
Upvotes: 2
Views: 14080
Reputation: 3256
The following line in your code will throw NoSuchElementException
on the first row that FindElement
doesnot not find an element with the matching criteria.
if (row.FindElement(By.XPath("//span[text()='" + tableRowIdentifier + "']")).Displayed)
Use FindElements() instead, as follows:
IWebElement matchedRow = null;
try
{
foreach(var row in rows)
{
if(row.FindElements(By.XPath("td/span")).FirstOrDefault(cell => cell.Text.Trim().Equals(TableRowIdentifier)) != null)
{
matchedRow = row;
break;
}
}
}
catch (NoSuchElementException)
{
//couldnot find
matchedRow = null;
}
if(matchedRow !=null)
{
cellValue = matchedRow.FindElement(By.XPath(string.Format("td[{0}]/span",targetCellIndex)).Text;
}
return cellValue;
Upvotes: 3