How To Iterate Table Using Selenium Webdriver


Most common Challenge Automation tester face during iterating through Table and list. they often want to find some value from the table cell or list and want to perform action on the same value or find corresponding other element in the same block and perform action on it.

Lets start with Simple Approach to Iterate a Web Table. In this Approach we are going to iterate using TD/TR. First of all we will find the location of table then we will store all table elements in the list and finally we will put a look  and iterate the Cells.


package IterateTable;

import static org.junit.Assert.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class IterateWebTable {
	  private WebDriver driver;
	  private String baseUrl;
	  private boolean acceptNextAlert = true;
	  private StringBuffer verificationErrors = new StringBuffer();
	public void setUp() throws Exception
		driver = new FirefoxDriver();
	    baseUrl = "";
	    driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
	public void test() {

	WebElement Webtable=driver.findElement("TableID")); // Replace TableID with Actual Table ID or Xpath
	List<WebElement> TotalRowCount=Webtable.findElements(By.xpath("//*[@id='TableID']/tbody/tr"));

	System.out.println("No. of Rows in the WebTable: "+TotalRowCount.size());
	// Now we will Iterate the Table and print the Values   
	int RowIndex=1;
	for(WebElement rowElement:TotalRowCount)
	      List<WebElement> TotalColumnCount=rowElement.findElements(By.xpath("td"));
	      int ColumnIndex=1;
	      for(WebElement colElement:TotalColumnCount)
	           System.out.println("Row "+RowIndex+" Column "+ColumnIndex+" Data "+colElement.getText());

	  public void tearDown() throws Exception {


Subscribe to Newsletter
Get Latest Posts, Articles and Learning Lessons on Software Testing and Test Automation
You can unsubscribe at any time

Comments are closed.