Software Development and QA Tips

Dev and QA Tips How Do You Manage a Web Table in Selenium WebDriver?

How Do You Manage a Web Table in Selenium WebDriver?

How Do You Manage a Web Table in Selenium WebDriver?

Web tables are typical HTML tables where data is presented in structural or tabular form on a website. Web tables can be static or dynamic, depending upon whether the rows and columns are fixed or dynamic.

To handle web tables in Selenium, you need the rows and columns count.

  • When the web table is static i.e., the rows and columns are clearly identified, teams can simply iterate over the data and parse/perform actions as below:

    for (int i =1; i<rows; i++)
    {
       for (int j=0; j<columns; j++)
       {
        here the xpath needs to //*[@id=/"table_id_or_available_attribute_class_etc/"]/tbody/tr[i]/td[j]
        }
    }
  • In the case of a dynamic table, you need to identify the rows and columns count first. Follow the below steps:

    • use List <WebElement>
      • Create an XPath for the table element till "/tr" to get rows count.
      • Similarly, create an XPath for table till "/th" to get columns count.
    • Once you have the rows and columns count, we can follow the same iteration as mentioned for static web table handling abov
    • A sample code for the dynamic web table would be:
      • List <WebElement> rows = webdriver.findElements(By.xpath("//table[@id=/"table_id_or_available_attribute_class_etc/"]/tbody/tr"));
      • List <WebElement> columns = webdriver.findElements(By.xpath("//table[@id=/"table_id_or_available_attribute_class_etc/"]/tbody/tr/th"));
      for (int i =1; i<rows.size(); i++)
      {
          for (int j=0; j<columns.size(); j++)
          {
          here the xpath needs to //* [@id=/"table_id_or_available_attribute_class_etc/"]/tbody/tr[i]/td[j]
          }
      }

Disclaimer

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.