Case I : Static WebTable
It means that the number of rows and columns are always constant, let’s say our html table will always have 3 rows and 3 columns as shown below i.e except row and column number, each component of xpath remains the same.
<div id="main">
<tbody>
<tr>
<th>FNAME</th>
<th>LNAME</th>
<th>AGE</th>
</tr>
<tr>
<td>UserFName1</td>
<td>UserLName1</td>
<td>26</td>
</tr>
<tr>
<td>UserFName2</td>
<td>UserLName2</td>
<td>27</td>
</tr>
</tbody>
</div>
So we can iterate and get the webelements as shown below:
for(int rn=1; rn<=3; rn++)
{
for(int cn=1; cn<=3; cn++)
{
System.out.println(driver.findElement(By.xpath(“//div[@id='main']/table[1]/tbody/tr[“+rn+”]/td[“+cn+”]”)));
}
}
where rn=Row number ad cn=Column numger
Case II: Dynamic WebTable
Above approach will not be a right solution for dynamically changing web tables, since the no. of rows and columns may vary.In that case we need to get the size of the rows and columns first and then we can iterate it over the webelements i.e:
Step 1: First get the entire html table and store this in a variable ‘table’ of type web element.
Step 2: Get all the rows with tag name ‘tr’ and store all the elements in a list of web elements. Now all the elements with tag ‘tr’ are stored in ‘rows’ list.
Step 3: Loop through each row and get the list of elements with tag ‘th’. ‘rows.get(0)’ will give first row and ‘findElements(By.tagName(“th”))’ will give list of columns for the row.
Step 4: Iterate using ‘columns.getsize()’ and get the details of each cell.
Let’s say in above html table we don't know that how many rows and columns are going to be present.So we need to handle it as below:
WebElement table=driver.findElement(By.xpath("//*[@id='main']/table[1]/tbody"));
List<WebElement> rows=htmltable.findElements(By.tagName("tr"));
for(int rn=0;rn<rows.size();rn++)
{
List<WebElement> columns=rows.get(rnum).findElements(By.tagName("th"));
System.out.println("Number of columns:"+columns.size());
for(int cn=0;cn<columns.size();cn++)
{
System.out.println(columns.get(cn).getText());
}
}
It means that the number of rows and columns are always constant, let’s say our html table will always have 3 rows and 3 columns as shown below i.e except row and column number, each component of xpath remains the same.
<div id="main">
<tbody>
<tr>
<th>FNAME</th>
<th>LNAME</th>
<th>AGE</th>
</tr>
<tr>
<td>UserFName1</td>
<td>UserLName1</td>
<td>26</td>
</tr>
<tr>
<td>UserFName2</td>
<td>UserLName2</td>
<td>27</td>
</tr>
</tbody>
</div>
So we can iterate and get the webelements as shown below:
for(int rn=1; rn<=3; rn++)
{
for(int cn=1; cn<=3; cn++)
{
System.out.println(driver.findElement(By.xpath(“//div[@id='main']/table[1]/tbody/tr[“+rn+”]/td[“+cn+”]”)));
}
}
where rn=Row number ad cn=Column numger
Case II: Dynamic WebTable
Above approach will not be a right solution for dynamically changing web tables, since the no. of rows and columns may vary.In that case we need to get the size of the rows and columns first and then we can iterate it over the webelements i.e:
Step 1: First get the entire html table and store this in a variable ‘table’ of type web element.
Step 2: Get all the rows with tag name ‘tr’ and store all the elements in a list of web elements. Now all the elements with tag ‘tr’ are stored in ‘rows’ list.
Step 3: Loop through each row and get the list of elements with tag ‘th’. ‘rows.get(0)’ will give first row and ‘findElements(By.tagName(“th”))’ will give list of columns for the row.
Step 4: Iterate using ‘columns.getsize()’ and get the details of each cell.
Let’s say in above html table we don't know that how many rows and columns are going to be present.So we need to handle it as below:
WebElement table=driver.findElement(By.xpath("//*[@id='main']/table[1]/tbody"));
List<WebElement> rows=htmltable.findElements(By.tagName("tr"));
for(int rn=0;rn<rows.size();rn++)
{
List<WebElement> columns=rows.get(rnum).findElements(By.tagName("th"));
System.out.println("Number of columns:"+columns.size());
for(int cn=0;cn<columns.size();cn++)
{
System.out.println(columns.get(cn).getText());
}
}