How to get row count in C#

2,915 views
Skip to first unread message

Kancharla Prasad

unread,
Sep 22, 2014, 7:40:38 AM9/22/14
to seleniu...@googlegroups.com
Hi All,

I am new to C# aswell to Selenium.

I am trying to write a code to get the row count from given table. but i couldn't find any (online) solution working find .
Could some one please provide me a code to get the row count, please find the attached screenshot of my HTML code

in many blogs i've seen use getXpathCount but i dnt see this option at all.

Thank you,
L V Prasad. K
Hwto get row count.png

Robin Gupta

unread,
Sep 22, 2014, 11:06:15 AM9/22/14
to seleniu...@googlegroups.com
Logical approach -
1. Write general xpath for all the rows in a table.
2. Use findElementsBy to get all the elements(rows) matching the above xpaths and store in a data structure.
3. Loop thru the data structure to get all the row elements.

There are many ways we can do this,
but in below example i am following "tr","td" approach.

WebDriver driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://money.rediff.com/");

WebElement element=driver.findElement(By.id("allpage_links"));
List<WebElement> rowCollection=element.findElements(By.xpath("//*[@id='allpage_links']/tbody/tr"));


System.out.println("Numer of rows in this table: "+rowCollection.size());
//Here i_RowNum and i_ColNum, i am using to indicate Row and Column numbers. It may or may not be required in real-time Test Cases.
int i_RowNum=1;
for(WebElement rowElement:rowCollection)
{
List<WebElement> colCollection=rowElement.findElements(By.xpath("td"));
int i_ColNum=1;
for(WebElement colElement:colCollection)
{
System.out.println("Row "+i_RowNum+" Column "+i_ColNum+" Data "+colElement.getText());
i_ColNum=i_ColNum+1;
}
i_RowNum=i_RowNum+1;
}
driver.close();

note - The above code is in Java but nevertheless LOGIc remains the same.

PeterJeffreyGale

unread,
Sep 22, 2014, 11:11:03 AM9/22/14
to seleniu...@googlegroups.com
Isn't there a findElements(...).size() method that gives a count of matching elements in a single line?

Jim Evans

unread,
Sep 22, 2014, 12:09:20 PM9/22/14
to seleniu...@googlegroups.com
Yes, there is. In Java:

// CSS selector would be something like "#ServiceLogResult > tr" (not tested for accuracy)
driver.findElements(By.xpath("//table[@id='ServiceLogResult'//tr")).size();

Same code in C#:

driver.FindElements(By.XPath("//table[@id='ServiceLogResult'//tr")).Count;

Mike NavMail

unread,
Sep 22, 2014, 1:08:07 PM9/22/14
to seleniu...@googlegroups.com
One thing to keep in mind, if your table has a grid header row, you'll want to filter that out somehow, otherwise your row count could be off.
 
Example using C# with Linq:
 

ReadOnlyCollection<IWebElement> tableRows = driver.FindElement(By.Id("tableIdValue")).FindElements(By.XPath("tbody/tr"));

return searchResultsRows.Where(searchResultsRow => !searchResultsRow.GetAttribute("class").Contains("grid_header")).Count;

 Mike

Madan Singh

unread,
Sep 23, 2014, 4:32:47 AM9/23/14
to seleniu...@googlegroups.com
Hi Please try this,

int rowcount=driver.FindElement(By.CssSelector("div#Results>table#ServiceLogResult>tbody").FindElements(By.TagName("tr")).Count;

I hope it will work.
 

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/a24b5ca0-0e83-496d-86c3-e303b1856c11%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
M P Singh
9971360313

Prasad.L.V

unread,
Sep 25, 2014, 3:35:04 AM9/25/14
to seleniu...@googlegroups.com
Thank you every one, got the row count.

--
You received this message because you are subscribed to a topic in the Google Groups "Selenium Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/selenium-users/34_sC1n3Lfs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to selenium-user...@googlegroups.com.

To post to this group, send email to seleniu...@googlegroups.com.

Arnab Patra

unread,
Apr 19, 2015, 9:21:05 AM4/19/15
to seleniu...@googlegroups.com
Hi Madan,
Can you please tell me why i am facing probel with your code.
VS2010 is telling me cannot convert method group "Count" to non deligate type 'int'


:( please help me

Madan Singh

unread,
Apr 20, 2015, 3:25:27 AM4/20/15
to seleniu...@googlegroups.com
Hi Arnab,

Can I see code what you written.
because there nothing special in my suggested code.





For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages