Software Testing Learning Hub

Selenium Lesson 11 – Handling Tables

Lesson 11 Tables

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.

we will iterate table in 3 steps

 Step no1  : Store table in list : We will store all elements in list so that we can iterate it

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

Step 2 : get row count : To get the no of rows in the table you have use following code:

System.out.println("No. of Rows in the WebTable: "+TotalRowCount.size());

Step 3 :  Iterate Table : Now for iteration, use following loop.

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());
	           ColumnIndex=ColumnIndex+1;
	       }
	      RowIndex=RowIndex+1;
	 }

 

Here is consolidated code which we have seen above.

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();
	@Before
	public void setUp() throws Exception
	{
		driver = new FirefoxDriver();
	    baseUrl = "http://Qeworks.com";
	    driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
	}
	@Test
	public void test() {

	WebElement Webtable=driver.findElement(By.id("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());
	           ColumnIndex=ColumnIndex+1;
	       }
	      RowIndex=RowIndex+1;
	 }

	}
	@After
	  public void tearDown() throws Exception {
	    driver.quit();
	}
}

we hope that this post will help you understand the basic iteration. if you have any queries feel free to ask.

Happy Learning!