0

I wish to grab an HTML table and extract the number of rows it has. I stumbled upon a solution linked here link. However, the proposed solution always returns row count of 1. I've alswo tried the IList<IWebElement> approach to get the length, but the result is the same. My table structure is pretty straightforward. Table's HTML is below.

How to get the total number of <tr>'s?

<table id="splitter_callbackWorkAreaPanel_workAreaUC_gvAdminArea_DXMainTable" class="dxgvTable" cellspacing="0" cellpadding="0" onclick="ASPx.GTableClick('splitter_callbackWorkAreaPanel_workAreaUC_gvAdminArea', event);" ondblclick="ASPx.GVTableDblClick('splitter_callbackWorkAreaPanel_workAreaUC_gvAdminArea', event);" oncontextmenu="return ASPx.GVContextMenu('splitter_callbackWorkAreaPanel_workAreaUC_gvAdminArea',event);" style="width:100%;border-collapse:collapse;empty-cells:show;table-layout:fixed;overflow:hidden;text-overflow:ellipsis;">
<tbody>
 <tr class="dxgvArm">
 <td style="width:28px;"></td>
 <td style="width:200px;"></td>
 <td style="width:210px;"></td>
 <td style="width:180px;"></td>
 <td style="width:100px;"></td>
 <td style="width:250px;"></td>
 <td style="width:180px;"></td>
 <td style="width:180px;"></td>
 <td style="width:180px;"></td>
 <td style="width:150px;"></td>
 <td style="width:170px;"></td>
 <td></td>
 </tr>
 <tr id="splitter_callbackWorkAreaPanel_workAreaUC_gvAdminArea_DXDataRow0" class="dxgvDataRow" style="">
 <td id="splitter_callbackWorkAreaPanel_workAreaUC_gvAdminArea_tccell0_0" class="dxgv" align="center" style="vertical-align:Middle;">
 <img src="/Client/images/7.jpg" alt="" width="14px" height="16px">
 </td>
 <td class="dx-nowrap dxgv">12/19/2018 12:05:24 PM</td>
 <td class="dx-nowrap dxgv">Osa test</td>
 <td class="dx-nowrap dxgv" align="right">28</td>
 <td class="dx-nowrap dxgv">.txt</td>
 <td class="dx-nowrap dxgv">Selenium test</td>
 <td class="dx-nowrap dxgv">&nbsp;</td>
 <td class="dx-nowrap dxgv">Normalno</td>
 <td class="dx-nowrap dxgv">&nbsp;</td>
 <td class="dx-nowrap dxgv">&nbsp;</td>
 <td class="dx-nowrap dxgv">&nbsp;</td>
 <td class="dxgvHEC"></td>
 </tr>
 <tr id="splitter_callbackWorkAreaPanel_workAreaUC_gvAdminArea_DXDataRow1" class="dxgvDataRow" style="">
 <td id="splitter_callbackWorkAreaPanel_workAreaUC_gvAdminArea_tccell1_0" class="dxgv" align="center" style="vertical-align:Middle;">
 <img src="/Client/images/9.jpg" alt="" width="14px" height="16px">
 </td>
 <td class="dx-nowrap dxgv">12/17/2018 1:55:45 PM</td>
 <td class="dx-nowrap dxgv">Osa test</td>
 <td class="dx-nowrap dxgv" align="right">22</td>
 <td class="dx-nowrap dxgv">.txt</td>
 <td class="dx-nowrap dxgv">d1</td>
 <td class="dx-nowrap dxgv">petar </td>
 <td class="dx-nowrap dxgv">Normalno</td>
 <td class="dx-nowrap dxgv">&nbsp;</td>
 <td class="dx-nowrap dxgv">&nbsp;</td>
 <td class="dx-nowrap dxgv">&nbsp;</td>
 <td class="dxgvHEC"></td>
 </tr>
 <tr id="splitter_callbackWorkAreaPanel_workAreaUC_gvAdminArea_DXDataRow2" class="dxgvDataRow" style="">
 <td id="splitter_callbackWorkAreaPanel_workAreaUC_gvAdminArea_tccell2_0" class="dxgv" align="center" style="vertical-align:Middle;">
 <img src="/Client/images/7.jpg" alt="" width="14px" height="16px">
 </td>
 <td class="dx-nowrap dxgv">12/14/2018 1:08:38 PM</td>
 <td class="dx-nowrap dxgv">Osa test</td>
 <td class="dx-nowrap dxgv" align="right">16</td>
 <td class="dx-nowrap dxgv">.txt</td>
 <td class="dx-nowrap dxgv">asdf</td>
 <td class="dx-nowrap dxgv">petar </td>
 <td class="dx-nowrap dxgv">Normalno</td>
 <td class="dx-nowrap dxgv">&nbsp;</td>
 <td class="dx-nowrap dxgv">&nbsp;</td>
 <td class="dx-nowrap dxgv">&nbsp;</td>
 <td class="dxgvHEC"></td>
 </tr>
 <tr id="splitter_callbackWorkAreaPanel_workAreaUC_gvAdminArea_DXDataRow3" class="dxgvDataRow" style="">
 <td id="splitter_callbackWorkAreaPanel_workAreaUC_gvAdminArea_tccell3_0" class="dxgv" align="center" style="vertical-align:Middle;">
 <img src="/Client/images/7.jpg" alt="" width="14px" height="16px">
 </td>
 <td class="dx-nowrap dxgv">12/14/2018 1:03:33 PM</td>
 <td class="dx-nowrap dxgv">Osa test</td>
 <td class="dx-nowrap dxgv" align="right">19</td>
 <td class="dx-nowrap dxgv">.txt</td>
 <td class="dx-nowrap dxgv">bnmbnm</td>
 <td class="dx-nowrap dxgv">petar </td>
 <td class="dx-nowrap dxgv">Normalno</td>
 <td class="dx-nowrap dxgv">&nbsp;</td>
 <td class="dx-nowrap dxgv">&nbsp;</td>
 <td class="dx-nowrap dxgv">&nbsp;</td>
 <td class="dxgvHEC"></td>
 </tr>
 <tr id="splitter_callbackWorkAreaPanel_workAreaUC_gvAdminArea_DXDataRow4" class="dxgvDataRow" style="">
 <td id="splitter_callbackWorkAreaPanel_workAreaUC_gvAdminArea_tccell4_0" class="dxgv" align="center" style="vertical-align:Middle;">
 <img src="/Client/images/7.jpg" alt="" width="14px" height="16px">
 </td>
 <td class="dx-nowrap dxgv">12/14/2018 12:48:38 PM</td>
 <td class="dx-nowrap dxgv">Osa test</td>
 <td class="dx-nowrap dxgv" align="right">18</td>
 <td class="dx-nowrap dxgv">.txt</td>
 <td class="dx-nowrap dxgv">vcxv</td>
 <td class="dx-nowrap dxgv">petar </td>
 <td class="dx-nowrap dxgv">Normalno</td>
 <td class="dx-nowrap dxgv">&nbsp;</td>
 <td class="dx-nowrap dxgv">&nbsp;</td>
 <td class="dx-nowrap dxgv">&nbsp;</td>
 <td class="dxgvHEC"></td>
 </tr>
 <tr id="splitter_callbackWorkAreaPanel_workAreaUC_gvAdminArea_DXDataRow5" class="dxgvDataRow" style="">
 <td id="splitter_callbackWorkAreaPanel_workAreaUC_gvAdminArea_tccell5_0" class="dxgv" align="center" style="vertical-align:Middle;">
 <img src="/Client/images/7.jpg" alt="" width="14px" height="16px">
 </td>
 <td class="dx-nowrap dxgv">12/14/2018 12:40:11 PM</td>
 <td class="dx-nowrap dxgv">Osa test</td>
 <td class="dx-nowrap dxgv" align="right">21</td>
 <td class="dx-nowrap dxgv">.txt</td>
 <td class="dx-nowrap dxgv">gfdg</td>
 <td class="dx-nowrap dxgv">petar </td>
 <td class="dx-nowrap dxgv">Normalno</td>
 <td class="dx-nowrap dxgv">ealo</td>
 <td class="dx-nowrap dxgv">&nbsp;</td>
 <td class="dx-nowrap dxgv">&nbsp;</td>
 <td class="dxgvHEC"></td>
 </tr>
 <tr id="splitter_callbackWorkAreaPanel_workAreaUC_gvAdminArea_DXDataRow6" class="dxgvDataRow" style="">
 <td id="splitter_callbackWorkAreaPanel_workAreaUC_gvAdminArea_tccell6_0" class="dxgv" align="center" style="vertical-align:Middle;">
 <img src="/Client/images/1.jpg" alt="" width="14px" height="16px">
 </td>
 <td class="dx-nowrap dxgv">12/7/2018 3:10:17 PM</td>
 <td class="dx-nowrap dxgv">Osa test</td>
 <td class="dx-nowrap dxgv" align="right">15</td>
 <td class="dx-nowrap dxgv">.txt</td>
 <td class="dx-nowrap dxgv">&nbsp;</td>
 <td class="dx-nowrap dxgv">petar </td>
 <td class="dx-nowrap dxgv">Normalno</td>
 <td class="dx-nowrap dxgv">&nbsp;</td>
 <td class="dx-nowrap dxgv">&nbsp;</td>
 <td class="dx-nowrap dxgv">&nbsp;</td>
 <td class="dxgvHEC"></td>
 </tr>
</tbody>

This is what I have tried so far:

int rowCount = Driver
 .chrome
 .FindElements(
 By.XPath("//[@id=\"splitter_callbackWorkAreaPanel_workAreaUC_gvAdminArea_DXMainTable\"]"))
 .Count();
var rowCount1 = Driver
 .chrome
 .FindElements
 (By.XPath("//*[@id=\"splitter_callbackWorkAreaPanel_workAreaUC_gvAdminArea_DXMainTable\"]/tbody"))
 .Count();
IList <IWebElement> table = Driver
 .chrome
 .FindElements(
 By.XPath("//*[@id=\"splitter_callbackWorkAreaPanel_workAreaUC_gvAdminArea_DXMainTable\"]"));
Moro
1,0528 silver badges18 bronze badges
asked Dec 20, 2018 at 12:59
11
  • can you show what you've tried Commented Dec 20, 2018 at 13:19
  • i have updated my answer check out it is accordingly the link you reffered Commented Dec 20, 2018 at 13:40
  • So far, I've tried these approaches: int rowCount = Driver.chrome.FindElements(By.XPath("//*[@id=\"splitter_callbackWorkAreaPanel_workAreaUC_gvAdminArea_DXMainTable\"]")).Count();<br/> var rowCount1 = Driver.chrome.FindElements(By.XPath("//*[@id=\"splitter_callbackWorkAreaPanel_workAreaUC_gvAdminArea_DXMainTable\"]/tbody")).Count(); <br/> IList <IWebElement> table = Driver.chrome.FindElements(By.XPath("//*[@id=\"splitter_callbackWorkAreaPanel_workAreaUC_gvAdminArea_DXMainTable\"]")); Commented Dec 20, 2018 at 13:44
  • this is because your xpath is till table and there is only 1 table so count will be 1 only Commented Dec 20, 2018 at 13:48
  • copy path till tr Commented Dec 20, 2018 at 13:48

2 Answers 2

1

Try this out

var table = driver.FindElement(By.TagName("tbody"));
var rows = table.FindElements(By.TagName("tr"));
foreach (var row in rows)
{
 Console.WriteLine(row); 
}

and as your link

driver.FindElements(By.XPath("//table[@id='splitter_callbackWorkAreaPanel_workAreaUC_gvAdminArea_DXMainTable'//tbody//tr")).Count;
answered Dec 20, 2018 at 13:17
0

Looking at what you have tried I see you did not count the number of rows, but a number of tables instead, that is why you always got 1 out of it.

To get the row count you can do as simple as this:

int rowCount = Driver
 .chrome
 .FindElements(
 By.CssSelector("table[class='dxgvTable'] tr")).Count();

tr is table row in html, so that is what you should be looking for.

answered May 20, 2019 at 8:16

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.