Validations of web table content are inevitable in automation testing because they represent data that is either inserted into the database via user input or they represent some information. on the web page. The data of a Web Table can either be static or dynamic in nature. In this article, I will show you how to handle the web tables in Selenium WebDriver to validate its content and perform various useful operations around it.
Before proceeding further let’s try to understand a Web Table.
Web Tables in Selenium WebDriver?
Likewise other web elements a web table is also a web table. A web table consists of rows, columns, and table-cell which contain the actual table data. A web table starts with a <table> tag and consists of the following HTML tags.
- <tbody> – Indicates body of the table
- < th > – Indicates heading of the table
- < tr > – Represents a row in a table
- < td > – Represents a column in a table
The following sample HTML code will give you a fair idea about the HTML structure of a web table.
The web table will look something like this.
Item | Price | Quantity |
---|---|---|
Shoe | 3000.00 | 50 |
Jacket | 5000.00 | 20 |
How to Get Web Table Headers in Selenium WebDriver?
We will refer to the following screenshot and the DOM structure.
The first cell value of the header can be retrieved using the following XPath.
1 2 3 4 5 6 7 8 |
//Getting value of first header item.Notice that we are using th[1] at the end of XPATH to get first value String firstHeaderValue = driver.findElement(By.xpath("//table[@id='tablepress-7']//thead/tr/th[1]")).getText(); //Getting value of first header item.Notice that we are using th[2] at the end of XPATH to get the second value String secondHeaderValue = driver.findElement(By.xpath("//table[@id='tablepress-7']//thead/tr/th[2]")).getText(); //Getting value of first header item.Notice that we are using th[3] at the end of XPATH to get third value String thirdHeaderVal = driver.findElement(By.xpath("//table[@id='tablepress-7']//thead/tr/th[3]")).getText(); |
How to Get a Cell Value of a Web Table in Selenium WebDriver?
I am going to show you an example of retrieving table cells for the first row and second column of a web table.
1 2 3 |
//Getting Cell value of first row and second column String cellValue = driver.findElement(By.xpath("//table[@id='tablepress-7']//tbody/tr[1]/td[2]")).getText(); |
How to Get Row Numbers of a Table in Selenium WebDriver?
It’s quite an easier thing to get row numbers of a table in Selenium WebDriver.You can use the below Selenium Code to get the row numbers. I have shown two different ways to get the row numbers of a table.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
/** ******* Getting row numbers in two different ways***** */ //***Approach 1*** //Creating table object WebElement table= driver.findElement(By.xpath("//table[@id='tablepress-7']//tbody")); //Creating object for all row elements except header List<WebElement> tableRows= table.findElements(By.tagName("tr")); //Getting row count/numbers int rowNumber=tableRows.size(); System.out.println("Table Row Numbers: "+rowNumber ); //***Approach 2: Directly creating a list of row elements**** List<WebElement> rowElements = driver.findElements(By.xpath("//table[@id='tablepress-7']//tbody/tr")); System.out.println("Row Numbers in the Table : "+rowElements.size()); |
How to Get Column Numbers/Count of a Table in Selenium WebDriver?
It’s not necessary that the developer would declare the table header using <th> Tag. So I will show you two ways to get table column numbers when the <th> tag is available and when it is not available.
Get Table Column numbers/count using Table Header Tag Object
have shown two different ways to get the row numbers of a table.
1 2 3 4 |
//In the above example we have created rowElements list object.Using the same to get column count List<WebElement> headerElements = driver.findElements(By.xpath("//table[@id='tablepress-7']//thead/tr/th")); System.out.println("Column Count: "+headerElements.size()); |
Get Table column numbers/count using Table Row Tag Object
It’s quite an easier thing to get row numbers of a table in Selenium WebDriver.You can use the below Selenium Code to get the row numbers. I have shown two different ways to get the row numbers of a table.
1 2 3 4 |
//In the above example we have created rowElements list object.Using the same to get column count List<WebElement> colElements=rowElements.get(1).findElements(By.tagName("td")); System.out.println("Column Numbers in the table are: "+colElements.size()); |
How to Handle Dynamic WebTables in Selenium Webdriver?
So far you have learned all the operations that can be performed on a web table in Selenium Webdriver. Now, here comes the dynamic table. Dynamic tables no longer required any special care. The only difference between a static table and a dynamic table is that the content of the static table does not change whereas the content of the dynamic table keeps on changing.
Dynamic tables might have dynamic links, text, images and etc on the table cells. The row numbers might increase or decrease after inserting and deleting some values. If you have to click any link or image on a dynamic table or set a value in the textbox that is inside a table cell, you can do these simply by locating the object by any of the locators like XPath, CssSelector, etc.
The challenges would come in scenarios like after entering some value on a page a new record gets inserted in the last row of the table and you will have to validate that. There could be a scenario wherein a record for a particular ID or serial number has been deleted from the table and you may have to check that record no longer exists on the table. So how to validate such things.
The solution is quite simple. You should first get the row and column count of the table and then iterate the entire table to validate whether the required record exists on the table or not.
How to Iterate a table in Selenium WebDriver?
In this example, I will iterate all the cells of the table and print them. You can use the same thing and tweak the logic according to your requirement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class IterateWebTableInSelenium { public static void main(String[] args) throws InterruptedException { //Setting the path of Chrome Browser Driver String BrowserDriverPath= "C:\\SeleniumBrowserDrivers\\chromedriver.exe"; //Setting System property for Chrome browser Driver. System.setProperty("webdriver.chrome.driver",BrowserDriverPath); //Create a new Instance for Chrome Browser WebDriver driver = new ChromeDriver(); driver.get("https://myskillpoint.com/handle-dynamic-web-tables-in-selenium-webdriver/"); List<WebElement> rowElements = driver.findElements(By.xpath("//table[@id='tablepress-7']//tbody/tr")); int iRowCount=rowElements.size(); List<WebElement> headerElements = driver.findElements(By.xpath("//table[@id='tablepress-7']//thead/tr/th")); int icolCount = headerElements.size(); for(int i=1;i<=iRowCount;i++) { System.out.println("Printing values for Row Number: "+ i); for(int j=1;j<=icolCount;j++) { System.out.println("Table Cell Values for row column ("+i+","+j+"): "+ driver.findElement(By.xpath("//table[@id='tablepress-7']//tbody/tr["+i+"]/td["+j+"]")).getText()); } } } } |
Output
Printing values for Row Number: 1
Table Cell Values for row column (1,1): Shoe
Table Cell Values for row column (1,2): 3000.00
Table Cell Values for row column (1,3): 50
Printing values for Row Number: 2
Table Cell Values for row column (2,1): Jacket
Table Cell Values for row column (2,2): 5000.00
Table Cell Values for row column (2,3): 20
How to Get Row Number for a Text in a Table in Selenium WebDriver?
In this example, I will show you how to find a required row number having a specific text(here we will look for Jacket) in any of the cells of that particular row.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class IterateWebTableInSelenium { public static void main(String[] args) throws InterruptedException { //Setting the path of Chrome Browser Driver String BrowserDriverPath= "C:\\SeleniumBrowserDrivers\\chromedriver.exe"; //Setting System property for Chrome browser Driver. System.setProperty("webdriver.chrome.driver",BrowserDriverPath); //Create a new Instance for Chrome Browser WebDriver driver = new ChromeDriver(); driver.get("https://myskillpoint.com/handle-dynamic-web-tables-in-selenium-webdriver/"); List<WebElement> rowElements = driver.findElements(By.xpath("//table[@id='tablepress-7']//tbody/tr")); int iRowCount=rowElements.size(); List<WebElement> headerElements = driver.findElements(By.xpath("//table[@id='tablepress-7']//thead/tr/th")); int icolCount = headerElements.size(); for(int i=1;i<=iRowCount;i++) { for(int j=1;j<=icolCount;j++) { if( driver.findElement(By.xpath("//table[@id='tablepress-7']//tbody/tr["+i+"]/td["+j+"]")).getText().equalsIgnoreCase("Jacket")) { int requireRowNumber=i; System.out.println("Required Row Number for text <<Jacket>> is: "+requireRowNumber ); } } } } } |
Output
Required Row Number for text <<Jacket>> is: 2
Recommended Posts
- XPath in Selenium WebDriver with Example
- Configure Selenium Webdriver With Eclipse
- How To Install TestNG in Eclipse
- Selenium Webdriver Login Test Script
- How to Select a Radio Button In Selenium WebDriver And Do Other Validations?
- Top #23 Most Useful Selenium WebDriver Commands That You Must know
- What is Selenium WebDriver & its Architecture
- How to Use AutoIT In Selenium WebDriver
- Read and Write Excel File in Java Using Apache POI Lib