How To Automate a Web Table in Selenium?

Timothy Joseph Timothy Joseph | March 7, 2022

How To Automate a Web Table in Selenium?

Web-tables are the tables displayed on the pages, which has data in the form of rows and columns.

There are two types of web-tables, and the very first thing that we need to know before automating is, whether the web-table is static or dynamic.

  1. Static Web-tables: Number of rows and columns is defined.
  2. Dynamic Web-tables: Rows and column count is unknown and can be anything.

Once we know, that our web-table is dynamic or static. We can follow below steps for automation of dynamic table:

  1. use List <WebElement>
    • Create an xpath for the table element till /tr and get a count of the same. (This will give us the row count)
    • Similarly for columns the path should be till /th (From here we will get columns count)
  2. Once, we get the row and column count, simply apply loop with i rows and j columns and perform your thing.
    • A sample code for the same would be:
      • List <WebElement rows = webdriver.findElements(By.xpath("//table[@id=/"table_id_or_available_attribute/"]/tbody/tr"));
      • List <WebElement cols = webdriver.findElements(By.xpath("//table[@id=/"table_id_or_available_attribute/"]/tbody/tr/th"));
      for (int i =1; i<rows.size(); i++)
          for (int j=0; j<cols.size(); j++)
          here the xpath needs to //*[@id=/"table_id_or_available_attribute/"]/tbody/tr[i]/td[j]

If the table is static, we can follow the same process, however, the row/cols count step can be omitted. The row-count/col-count can be directly used in the above loop.
For better QA services we can make the XPath dynamic as mentioned inside the loop.

New call-to-action


This publication is for informational purposes only and nothing contained in it should be considered legal advice. We expressly disclaim any warranty or responsibility for damages arising out of this information and encourage you to consult with legal counsel regarding your specific needs. We do not undertake any duty to update previously posted materials.

Post a Comment