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\"]"));
  • can you show what you've tried – Rao Dec 20 '18 at 13:19
  • i have updated my answer check out it is accordingly the link you reffered – Rao Dec 20 '18 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\"]")); – Nikola Pepic Dec 20 '18 at 13:44
  • this is because your xpath is till table and there is only 1 table so count will be 1 only – Rao Dec 20 '18 at 13:48
  • copy path till tr – Rao Dec 20 '18 at 13:48
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;
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.