Question

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

Was it helpful?

Solution

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.

OTHER TIPS

Try this to get the number of Rows in a Table

int iRowsCount = driver.FindElements(By.XPath("/html/body/..../table/tbody/tr")).Count;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top