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]
}
}
- use List <WebElement>
Post a Comment