Number of rows in a table using Selenium C#

11,217

Solution 1

If it's the only table with rows marked with those class names, you could just use:

decimal numOfOddRows = selenium.GetXpathCount("//tr[@class='v-table-row-odd']"); // 3
decimal numOfEvenRows = selenium.GetXpathCount("//tr[@class='v-table-row']");    // 3
decimal numOfRows = numOfOddRows + numOfEvenRows;                        // 3 + 3 = 6

If not, then you need to find a better way of locating the table. Something is not quite right with your (very long) XPath selector which starts from the very top of the document. There's nothing inherently wrong about this, but with a dynamic webpage it is very hard to get right.

Instead, you need to locate an element closer to your table and then filter within that. If for instance, if one of your divs has a name attribute, you could use //div[@name='someName']//tr. For more information about using XPath selectors, see here.

Solution 2

Try this to get the number of Rows in a Table

int iRowsCount = driver.FindElements(By.XPath("/html/body/..../table/tbody/tr")).Count;
Share:
11,217
sai sindhu
Author by

sai sindhu

Updated on November 21, 2022

Comments

  • sai sindhu
    sai sindhu over 1 year

    I am new to Selenium. I am working with c#. There is a table like this . I saw it using FireBug.

    <table>
      <tbody>
        <tr class="v-table-row-odd"></tr>
        <tr class="v-table-row"></tr>
        <tr class="v-table-row-odd"></tr>
        <tr class="v-table-row"></tr>
        <tr class="v-table-row-odd"></tr>
        <tr class="v-table-row"></tr>
      </tbody>
    </table>
    

    And the issue here is I am not knowing how to get the number of rows in a table which changes dynamically. Is there any way??

    Tried xpathCount but got some exception issues..

    decimal numOfRows = selenium.GetXpathCount("xpath=/html/body/div/div/div[2]/div/div[3]/div/div[2]/div/div/div[2]/div/div/div/div/div[7]/div/div/div[2]/div/table/tbody/tr");
    

    I also tried xpathCount like this

    selenium.GetXpathCount("xpath=/html/body/div/div/div[2]/div/div[3]/div/div[2]/div/div/div[2]/div/div/div/div/div[7]/div/div/div[2]/div/table/tbody");
    

    But both raised exceptions. Can anyone help me out in this regard.

    Thank You

    • Misha Akovantsev
      Misha Akovantsev about 12 years
      Which exceptions? Chances are you got your xpath expressions wrong.
    • faramka
      faramka about 12 years
      Has any div in this xPath got an id or any other unique value of an attribute? If you want to get number of rows, you should use something like the first xPath (ending with /table/tbody/tr) rather than only /table/tbody, because the second will count number of tbody in table - so it will be 1).
    • Ross Patterson
      Ross Patterson about 12 years
      Those are some really lousy XPath selectors. They'll give you no end of trouble later on when the page structure changes a little bit. But the real problem you have is that GetXpathCount() only works on XPath selectors, so it doesn't expect the selector to begin with xpath=. Remove that and what you've coded will work, at least for now.
  • Ripon Al Wasim
    Ripon Al Wasim over 6 years
    It's better to count <tr> inside <tbody> by using int iRowsCount = driver.FindElements(By.XPath("//table/tbody/tr")).Count;