How to find the number of rows and columns in table in selenium ?

In this post we will learn the how to fetch the number of columns and rows of the table  and it is the most common interview question asked in the selenium interviews. We may find many situation where we have to verify the data present in the HTML tables so we will study now how to read the data from tables in JAVA selenium.

Lets take the below table example to read the whole data in selenium webdriver

1 Jack 9
2 Mark 10
3 Siya 6
4 Ram 7

HTML code of the Table

<table class="easy-table easy-table-cuscosky ">
<tbody>
<tr>
<td>1</td>
<td>Jack</td>
<td>9</td>
</tr>
<tr>
<td>2</td>
<td>Mark</td>
<td>10</td>
</tr>
<tr>
<td>3</td>
<td>Siya</td>
<td>6</td>
</tr>
<tr>
<td>4</td>
<td>Ram</td>
<td>7</td>
</tr>
</tbody>
</table>

Now we will try to read the value of Mark from the table . While reading the values of “td” we don’t have any class or attribute so we have to use the Xpath for that.

Now let us make the Xpath for the value “Mark”
Location of “Mark” -> 2nd Row and 2nd column
Xpath = //table/tbody/tr[2]/td[2]

Count number of rows and columns of table

Find number of Rows of table :

List<WebElement> rows = driver.findElements(By.xpath
				("html/body/div/div[1]/div/main/article/div/table/tbody/tr"));
		int NumberOfRows = rows.size();

Find number of columns of table:

List<WebElement> columns =  driver.findElements(By.xpath
				("html/body/div/div[1]/div/main/article/div/table/tbody/tr[1]/td"));
		int NumberOfColumns = columns.size();

Below is the complete program to fetch the value from the table and count the number of rows and columns and print all table data in JAVA ( selenium )

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
		
public class Read_Table_data {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		WebDriver driver;
		System.setProperty("webdriver.gecko.driver",
				System.getProperty("user.dir")+ "\\driver\\geckodriver.exe");
		driver = new FirefoxDriver();
		driver.navigate().to
		("https://padhle.com/find-the-rows-and-columns-in-table-selenium/");
		
		//get the Mark value 2nd row 2ndcolumn value
		String value = driver.findElement(By.xpath
				("//table/tbody/tr[2]/td[2]")).getText();
		System.out.println("value in 2nd row and 2nd column : " + value);

		//fetching all rows
		List rows = driver.findElements(By.xpath
				("html/body/div/div[1]/div/main/article/div/table/tbody/tr"));
		int NumberOfRows = rows.size();
		
		//fetching all columns
		List columns =  driver.findElements(By.xpath
				("html/body/div/div[1]/div/main/article/div/table/tbody/tr[1]/td"));
		int NumberOfColumns = columns.size();
		
		 //divided xpath In three parts to pass Row_count and Col_count values.
		 String first_part = "//table/tbody/tr[";
		 String second_part = "]/td[";
		 String third_part = "]";

		 System.out.println("number of row : " + NumberOfRows);
		 System.out.println("number of columns : " + NumberOfColumns);
		 
		// print all table data
		 System.out.println("print all table data ");
		 for (int i=1;i<=NumberOfRows;i++)
		{
			for (int j=1;j<=NumberOfColumns;j++)
			{
				
				String final_xpath = first_part+i+second_part+j+third_part;
				String Table_data = driver.findElement
							(By.xpath(final_xpath)).getText();
				
				System.out.print(Table_data +"  ");
				
			}
			System.out.print(" \n");
		}
		
		driver.close();
		
	}	
	
	}
	

Output of the Program

value in 2nd row and 2nd column : Mark
number of row : 4
number of columns : 3
print all table data 
1  Jack  9   
2  Mark  10   
3  Siya  6   
4  Ram  7

Hope you are now clear with the how to read the data from HTML tables in websites in JAVA selenium. If you have any doubt feel free to comment in below section.

Leave a Comment

Your email address will not be published. Required fields are marked *