There are two types of HTML tables published on the web-
Static tables: Data is static i.e. Number of rows and columns are fixed.
Dynamic tables: Data is dynamic i.e. Number of rows and columns are NOT fixed.
In this article, we will see two methods of handling dynamic web tables through Selenium.
Let’s consider the following link:
Login with the following credentials:
Username : Admin | Password : admin123
Click on PIM tab and navigate to Employee List.
We will get a list of employees which is stored in a dynamic web table. We are supposed to search an employee with first name as ‘Lisa’ and the click on the checkbox corresponding to this employee as shown below:
![](https://static.wixstatic.com/media/348967_4b8fe9e8c267419bbf88c89385bbeff1~mv2.png/v1/fill/w_975,h_509,al_c,q_90,enc_auto/348967_4b8fe9e8c267419bbf88c89385bbeff1~mv2.png)
The code till logging in the portal and going to the employee list is common for both the methods. Also, note that here we are assuming that the employee is found on the first page itself and not taking pagination into consideration for the time being.
This can be achieved with the following code:
![](https://static.wixstatic.com/media/348967_729bc7fe324c443fa40ceedb7f4b7d01~mv2.png/v1/fill/w_975,h_509,al_c,q_90,enc_auto/348967_729bc7fe324c443fa40ceedb7f4b7d01~mv2.png)
Method — 1:
Let us inspect the web element:
![](https://static.wixstatic.com/media/348967_d2121ce2ba594b5ead7a921f165b2ff8~mv2.png/v1/fill/w_975,h_471,al_c,q_90,enc_auto/348967_d2121ce2ba594b5ead7a921f165b2ff8~mv2.png)
What we see is that all the employee list is in a web table with id = resultTable. The employee first name and the checkbox are the table data in different columns within the same row.
Now lets us see how we can traverse to these web elements via different methods.
We will first get the total number of rows in the table on the page. This can be achieved by calculating the total tr tags inside the table with the following XPath:
"//table[@id='resultTable']//tr"
Please note that the total rows would also include the heading row.
So now using a loop, we will traverse each row one by one. Inside the loop, we will put an if condition to search for the employee ‘Lisa’ and then go to the corresponding checkbox to click and hence check it.
Please find below the code for Method 1:
![](https://static.wixstatic.com/media/348967_05433064060f4463ac1123eba809ad35~mv2.png/v1/fill/w_975,h_518,al_c,q_90,enc_auto/348967_05433064060f4463ac1123eba809ad35~mv2.png)
When you run this code, you will see that the checkbox corresponding to employee with first name as Lisa has been checked as shown below:
![](https://static.wixstatic.com/media/348967_b9759cdfded046de8b95334a5292684a~mv2.png/v1/fill/w_975,h_494,al_c,q_90,enc_auto/348967_b9759cdfded046de8b95334a5292684a~mv2.png)
This method will iterate the entire table and hence lengthier and will take longer time to execute.
Method — 2:
In the second method we will create a custom XPath for the employee with first name Lisa.
Let’s once again inspect the web element.
![](https://static.wixstatic.com/media/348967_b35b5b9a4bc24fcab313011cd31ab178~mv2.png/v1/fill/w_975,h_471,al_c,q_90,enc_auto/348967_b35b5b9a4bc24fcab313011cd31ab178~mv2.png)
The employee first name is under the anchor tag. So, we jump to that tag which contains the name Lisa like this:
//a[contains(text(),'Lisa')]
![](https://static.wixstatic.com/media/348967_21c1d7bdd5514e42a2c339ed11669708~mv2.png/v1/fill/w_975,h_372,al_c,q_90,enc_auto/348967_21c1d7bdd5514e42a2c339ed11669708~mv2.png)
From here we go to its parent td tag
//a[contains(text(),'Lisa')]/parent::td
![](https://static.wixstatic.com/media/348967_76d3763878644671ba5e0c02f4bfb0f7~mv2.png/v1/fill/w_975,h_322,al_c,q_85,enc_auto/348967_76d3763878644671ba5e0c02f4bfb0f7~mv2.png)
From here we then go to its preceding sibling td tag. Note that we will get 2 preceding siblings, but we have to select the one for which the input type is a checkbox:
//a[contains(text(),'Lisa')]/parent::td//preceding-sibling::td//input[@type='checkbox']
![](https://static.wixstatic.com/media/348967_c0454146a1e749f0a90a30bf47b687a5~mv2.png/v1/fill/w_975,h_366,al_c,q_85,enc_auto/348967_c0454146a1e749f0a90a30bf47b687a5~mv2.png)
Now, just click it to check the checkbox and we will get the desired output.
Here is the code for this method:
![](https://static.wixstatic.com/media/348967_1acef7c05e344a23a8a23cc907e7930e~mv2.png/v1/fill/w_975,h_400,al_c,q_90,enc_auto/348967_1acef7c05e344a23a8a23cc907e7930e~mv2.png)
And here is the output:
![](https://static.wixstatic.com/media/348967_5370a26c7864419fa361c1cb5eaab78c~mv2.png/v1/fill/w_975,h_499,al_c,q_90,enc_auto/348967_5370a26c7864419fa361c1cb5eaab78c~mv2.png)
We no longer require any loop and there is no need to even iterate the entire table. Look at the simplicity of the code, with just one single line of code without using any loop we are able to achieve the desired result. This method is more efficient and faster when it comes to handling dynamic web tables.
Hope this article was helpful to you.
Thanks for reading!!!
great