Aishu Aishu - 1 year ago 86
C# Question

Handling tables in c# selenium

<table cellpadding="0" cellspacing="0" onclick="" style="width: 1345px;">

<tbody>

<tr id="item_tcm:222-382904-131104" title="2. Publish to WIP (tcm:222-382904-131104)" class="item even" c:drawn="true">
<td class="col0 icon odd" value="T131104L0P0">
<div class="icon" style="background-image: url(&quot;/WebUI/Editors/CME/Themes/Carbon2/icon_v7.1.0.66.55_.png?name=T131104L0P0&amp;size=16&quot;);"></div>
</td>
<td class="col1 even">
<div class="text">2.&nbsp;Publish&nbsp;to&nbsp;WIP</div>
</td>
<td class="col2 odd">
<div class="text">JH&nbsp;Anchor&nbsp;link&nbsp;2</div>
</td>
<td class="col3 even">
<div class="text">S070&nbsp;Public&nbsp;Site&nbsp;US&nbsp;English</div>
</td>
<td class="col4 odd" value="2015-12-23T14:41:04">
<div class="text">12/23/2015 2:41 PM</div>
</td>
<td class="col5 even">
<div class="text">NT&nbsp;AUTHORITY\SYSTEM</div>
</td>
<td class="col6 odd" value="">
<div class="text">
<span style="color: #f00"></span>
</div>
</td>
<td class="col7 even" value="16">
<div class="text">Suspended</div>
</td>
<td class="col8 odd">
<div class="text">NT&nbsp;AUTHORITY\SYSTEM</div>
</td>
<td class="col9 even">
<div class="text">Publishing&nbsp;Failed</div>
</td>
</tr>
<tr id="item_tcm:222-382901-131104" title="2. Publish to WIP (tcm:222-382901-131104)" class="item even" c:drawn="true">
<td class="col0 icon odd" value="T131104L0P0">
<div class="icon" style="background-image: url(&quot;/WebUI/Editors/CME/Themes/Carbon2/icon_v7.1.0.66.55_.png?name=T131104L0P0&amp;size=16&quot;);"></div>
</td>
<td class="col1 even">
<div class="text">2.&nbsp;Publish&nbsp;to&nbsp;WIP</div>
</td>
<td class="col2 odd">
<div class="text">JH_anchor&nbsp;link</div>
</td>
<td class="col3 even">
<div class="text">S070&nbsp;Public&nbsp;Site&nbsp;US&nbsp;English</div>
</td>
<td class="col4 odd" value="2015-12-23T14:17:51">
<div class="text">12/23/2015 2:17 PM</div>
</td>
<td class="col5 even">
<div class="text">NT&nbsp;AUTHORITY\SYSTEM</div>
</td>
<td class="col6 odd" value="">
<div class="text">
<span style="color: #f00"></span>
</div>
</td>
<td class="col7 even" value="16">
<div class="text">Suspended</div>
</td>
<td class="col8 odd">
<div class="text">NT&nbsp;AUTHORITY\SYSTEM</div>
</td>
<td class="col9 even">
<div class="text">Publishing&nbsp;Failed</div>
</td>
</tr>
.....
</tbody>
</table>


I have collection of rows. Inside each row i have 10 columns(td). I want to iterate to each row. For each row I want to get the 8th and 10 th column.
Note :- The test case will get Fail if the 8th column value is "Suspended" and 10th column value is "Publishing Failed" or else the test case would get Pass

I tried the below logic

IWebElement tableElement = driver.FindElement(By.XPath("/html/body/table"));
IList<IWebElement> tableRow = tableElement.FindElements(By.TagName("tr"));
foreach (var item in tableRow)
{

}


I'm not sure how to proceed further. Could anyone help me? Thanks in advance

Answer Source

Your logic is good:

IWebElement tableElement = driver.FindElement(By.XPath("/html/body/table"));
IList<IWebElement> tableRow = tableElement.FindElements(By.TagName("tr"));
IList<IWebElement> rowTD;
foreach (IWebElement row in tableRow)
{
   rowTD = row.FindElements(By.TagName("td"));

   if(rowTD.Count > 9)
   {
      if(rowTD[8].Text.Equals("Suspended") && rowTD[10].Text.Equals("Publishing Failed");
      //test failed
   }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download