Count total rows in a table

Hi guys,

I want to know how can I count total rows in the table. here is the example of the web structure (the picture just shown the table section, not the whole web)

the
result of count should be 9, but when I tried to count it, the result =
55 (regardless of the web’s overall structure). so I tried to modify the web to check it with deleting 2 row of , and count again. but it was always shown me result of 55 again (the result that I expected should be 7 because 2 row was deleted)

I tried 2 variant of code :
1. ArrayList forms = driver.findElements(By.cssSelector("[class=‘mGrid’] tbody tr"))
-> the result = 55

2. ArrayList forms = driver.findElements(By.cssSelector(’#gvPaymentAllocationDetail tr’))
-> the result = 0

Do you have any idea to solve my problem? Any help from you will be great for me :slight_smile:

tanya.png

Hello,
there is user keyword that can execute xpath count for you and return number of rows to you:

otherwise, you need to write function like here:

Hello Andrej, thanks for the suggestion.

Unfortunately, it still didn’t work for me, the result just same to shown result 55 or 0 depend on method I wrote before.
Any another idea or maybe a logic which suits to my case?

following is not working?

By.cssSelector('#gvPaymentAllocationDetail > tr')

in xpath:

By.xpath('id("gvPaymentAllocationDetail")//tr')

2. ArrayList forms = driver.findElements(By.cssSelector(‘#gvPaymentAllocationDetail tr’))
-> the result = 0

your CSS Selector look OK.

I doubt there could be some timing problem. The table is dynamically updated by JavaScript, isn’t it? Then you need to wait for the table to be present and visible and filled with rows before you get access to it.

1. ArrayList forms = driver.findElements(By.cssSelector(“[class=‘mGrid’] tbody tr”))
-> the result = 55

I doubt you have multiple tables with class=‘mGrid’ in the web page other than the one you are looking at now.

<table class='mGrid'>
...
</table>
...
<table class='mGrid'>
...
</table>
...

Or your table may contain nested tables in s.

<table class='mGrid'>
<tr><td><table class='mGrid'>...</table></td></tr>
</table>

@Andrej Podhajský
yep, unfortunately it doesn’t work, the result just shown me result = 0

@4280-kazurayam
I think that it wasn’t the problem. I already make the delay and wait until the page completely loaded, but the result still = 0.
and for the multiple table with the same class name, I already check it too with inspect element and search on it, and the result shown only 1 class with name ‘mGrid’

Do you have any idea? Maybe another possible script/logic to count the rows?

kazurayam said:

Or your table may contain nested tables in s.

<table class='mGrid'>
...
> ``` > >

or nested ?
can you please send html of that table with whole structure?

Difficult issue without immediate access to the target HTML. Can you disclose the url? Or can you attach here the target HTML file as a whole.

@Andrej Podhajský

I feel bad that I can’t send the whole html cause of my company permission. I can only give the section with expanded tag structure

the whole tag will be same like this. I hope even with this limitation there will still any possible solution for me

ask.png

Willis Williandy said:

Hi guys,
*** cut ***
so I tried to modify the web to check it with deleting 2 row of , and count again. but it was always shown me result of 55 again (the result that I expected should be 7 because 2 row was deleted)

any iframes involed?

@4280-kazurayam
Like I said before, feel bad, I can’t share the whole html due to company permission. as the replacement, I tried to give the expanded html structure that I worked with.

@Andrej Podhajský
nope, there are just a basic table like usual. I’m getting crazy about this, cause it successfully worked to the other web I worked before, but not this one now haha

just firing blind… is driver pointing to correct page? aren’t there any leftovers from previous code?

one thing … did you try those selectors in some browser plugin (firebug/firepath) to see what exactly you are selecting?

yes for sure it is pointing to the correct page and no any leftovers

I also do it with firebug and all of the element and object are selected correctly.

Actually I tried this method to get the total rows to be used for my looping variable. Now I think I will forget to count the rows and just using static variable for it :s

btw, thanks for the suggestion, I really appreciate it :slight_smile: