Storing all the values appearing a table column

Hi All,

I’ve a query on HTML table handling. In a page a HTML table is loading which has 6 columns. Table has huge data. I want to pick all the values appearing on 2 column and create a list out of it.
Then want to pass each value to validate against the db value.
Can anyone guide me how to achieve it?

Thanks,
Vivek K

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

Idea 1 - Get WebElements

@{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}

Idea 2 - Get Element Count

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.

Hi Dave,

Thanks for the solution. For Part 2 question -
[Que] 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?
[Ans] No. I need to capture all the values from 2 column and store in a list to pass each one as parameter to DB query.

It would be very helpful if you provide a code snippet for Part 2.

Thanks,
Vivek K

Hi Vivek,

Actually for part 2 I gave you all the keywords you need for both Options, everything except the database query, but for that you’ll need to work out what sort of database you are using and find an appropriate library and as I don’t know the data base structure of you application I can’t really help with the query, but maybe your DBA or Developers can help with that.

All I can suggest is the Database Library will probably be useful of your application uses a traditional SQL database, if it uses a NoSQL style database you’ll need something else and that will depend on the type of database.

I linked the documentation for all the keywords, the documentation has lots of example on how to use them, pick the example that works best for you situation. You’ll learn a lot more this way than if I write the example for you.

Dave.

Sure Dave. Thanks I’ll work on the DB and code you provided.