Handling Web Tables in Selenium

  • This requires some knowledge of HTML table. The table is the main tag where we have rows< tr >, and one row can have many columns < td >.

  • Sample table will be as given below. Write the code in notepad and save the file with any name and the extension should be html. Let’s say sample.html, open file in browser

  • The table needs to be automated, means we should be able to print all values in a table. In future we can test those values against expected values.

  • Now if we go about locating every element of a table, it’s not affordable as we don’t know how many rows will be there. Columns are fixed, but number of rows is not.

  • Need to write the same code again and again if dynamic code is not written.

  • There may be cases where we may not have any rows.

  • So we need to put some logic here as below:

  • First locate the table, and then check how many rows are there.

  • Iterate over rows. Find columns in every row. Iterate over columns and print data dynamically.

web tables example in selenium

Complete code of above table: