Sunday 23 December 2018

How to Identify Table in a webpage in a easiest way using Xpath

Identify Table in a webpage using Xpath and click a required Cell in a table


Hello Everyone , Today we will be learning about how we can identify a table or many table present in a webpage easiest way just using XPATH.


First lets Start with Basics.

//table : It will identify all the table present in a webpage.
//thead : It will identify header of a particular table.
//tbody : It will identify body of a particular table.
//tr : Selects the row.
//td: Selects the cell.
//th : Selects the header cell. 


 For any query how wewrite a Xpath please look into my previous post : 
How to get Xpath of elements in webdriver


 Now lets suppose we have to identify a table in a website.if only one table is present in the website the our task is easy as we don't have to uniquily identify the table as only one table is present in the current webpage. In this case following Xpath will work if we want to click or get text of a particular cell.

                                               Xpath : //table/tbody/tr[2]/td[2]




The above xpath will get the cell value of 2nd row and 2nd column in table body. Similarly we can identify any cell by giving the index value of row and column as per requirement.

Now , Task really starts when we have more than one table in a web page and we really struggle to identify the table which we are looking for. So for that index help us to get it done very easily.  


Suppose we are having two table in a webpage, so for identifying them we have to take the index of each table. Generally we have index of 1st table in webpage as [1] and index of second table as [2]. See below example:


                                                  Xpath: //table[index]


Here we have used index as 2 to identify table 2 in a webpage.once we have selected the table all other operations will remain same to identify cell, column or row in a table.

Hope you understand how we can identify different table in a webpage using this simple Xpath trick.