Hi Vivek,
There are a few ways you can approach this, remember there is never 1 “right” way, so i’ll give a few suggestions but i’m sure there’ll be other ways too.
You didn’t mention which library you’re using, so I’ll use SeleniumLibrary, but these approaches could be used with other libraries as well.
As I don’t know your html and whether it display’s the table on the page with a html table or with div’s, I’ll use this page as a reference as it uses a html table
Part 1
@{myelements}= Get WebElements //tr/td[2]
This will give you a list of webelements, you can do a Get Length to use for in range or simply iterate over the list with for ${element} in @{myelements}
You can use Get Element Count to get a count of matching elements and then use for in range with that count to get the values of the individual cells
${count}= Get Element Count //tr/td[2]
FOR ${index} IN RANGE ${count}
${value}= Get Text (//tr/td[2])[${index}]
END
Part 2
so that gives you 2 ways to get the values from the table, now for the second part of your question creating a list out of it and comparing to the database results
One Option
- Create a list as you mentioned, before the for statement use Create List will let you create an empty list.
- Then use Append To List from Collections Library in the for loop to populate the list.
- query the database to return the list you expect will match the list on the page
- use Lists Should Be Equal to compare the database list to the html list
But this begs the question, if you need to check the values in column 2, do you need to also check the values in the other columns?
Another Option
create a keyword that checks all the column’s data against the database, then place that keyword in the for loop, the argument to your new keyword could be either the row number or of you Get WebElements
could be the whole table row
Combining either option from part 2 with either idea from part 1 gives you a few ways to approach it, i’ll let you decide whats best for your application.
Hope that helps,
Dave.