sai sindhu
sai sindhu

Reputation: 1155

Number of rows in a table using Selenium C#

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

Upvotes: 1

Views: 13125

Answers (3)

Daneesha
Daneesha

Reputation: 425

What worked for me was a combination of XPath with ID and the table tbody tr elements added at the end.

int RowCount = driver.FindElements(By.XPath("//*[@id='tableID']/tbody/tr")).Count;

Upvotes: 1

shamp00
shamp00

Reputation: 11326

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.

Upvotes: 3

Madhu
Madhu

Reputation: 497

Try this to get the number of Rows in a Table

int iRowsCount = driver.FindElements(By.XPath("/html/body/..../table/tbody/tr")).Count;

Upvotes: 4

Related Questions