How To Handle Dynamic Web Table In Selenium WebDriver

If web table has same number of rows and same number of cells In each rows every time you load page then It Is very easy to handle that table's data In selenium WebDriver software testing tool as described In How To Extract Table Data/Read Table Data Using Selenium WebDriver post. Now supposing your table's rows and columns are increasing/decreasing every time you loading page of software web application or some rows has more cells and some rows has less cells then you need to put some extra code In your webdriver software test case which can retrieve cell data based on number of cells In specific row. Consider the dynamic webtable shown In bellow given Image.


In this table, Row number 1, 2 and 4 has 3 cells, Row number 3 has 2 Cells and Row 5 has 1 cell. In this case, You need to do some extra code to handle these dynamic cells of different rows. To do It, You need to find out the number of cells of that specific row before retrieving data from It.

You can view more webdriver software testing tutorials with testng and java WEBDRIVER TUTORIAL @PART 1 and WEBDRIVER TUTORIAL @PART 2.

Bellow given example will first locate the row and then It will calculate the cells from that row and then based on number of cells, It will retrieve cell data Information.

Run bellow given example In your eclipse with testng which Is designed for above given dynamic web table.
package Testng_Pack;

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

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class dynamic_table {
 WebDriver driver = null;

 @BeforeTest
 public void setup() throws Exception {
  System.setProperty("webdriver.gecko.driver", "D:\\Selenium Files\\geckodriver.exe");
  driver = new FirefoxDriver();
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
  driver.get("http://only-testing-blog.blogspot.com/2014/05/form.html");
 }

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

 @Test
 public void Handle_Dynamic_Webtable() {
 
  //To locate table.
  WebElement mytable = driver.findElement(By.xpath(".//*[@id='post-body-8228718889842861683']/div[1]/table/tbody"));
  //To locate rows of table.
  List<WebElement> rows_table = mytable.findElements(By.tagName("tr"));
  //To calculate no of rows In table.
  int rows_count = rows_table.size();
  
  //Loop will execute till the last row of table.
  for (int row=0; row<rows_count; row++){
   //To locate columns(cells) of that specific row.
   List<WebElement> Columns_row = rows_table.get(row).findElements(By.tagName("td"));
   //To calculate no of columns(cells) In that specific row.
   int columns_count = Columns_row.size();
   System.out.println("Number of cells In Row "+row+" are "+columns_count);
   
   //Loop will execute till the last cell of that specific row.
   for (int column=0; column<columns_count; column++){
    //To retrieve text from that specific cell.
    String celtext = Columns_row.get(column).getText();
    System.out.println("Cell Value Of row number "+row+" and column number "+column+" Is "+celtext);
   }
   System.out.println("--------------------------------------------------");
  }  
 }
}

Above example will works for dynamic changing web table too where number of rows changing every time you load the page or search for something.

8 comments:

  1. Webdriver dynamic webtable text matches:

    package day7;
    import java.util.List;
    import java.util.concurrent.TimeUnit;
    import org.openqa.selenium.By;
    import org.openqa.selenium.Keys;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;

    public class WebTableSelectTextClass
    {

    /**
    * @param args
    * @throws Throwable
    */
    public static void main(String[] args) throws Throwable {
    WebDriver driver = new FirefoxDriver();
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    driver.get("http://erail.in");

    // enter data
    driver.findElement(By.id("txtStationFrom")).sendKeys("MAS", Keys.TAB);
    driver.findElement(By.id("txtStationTo")).sendKeys("SBC", Keys.TAB);

    webtableclass (driver, "//*[@id='divTrainsListTrainsObj']/table", "YPR", "a", 1);
    Thread.sleep(15000);
    driver.close();
    driver.quit();
    }


    public static void webtableclass(WebDriver driver, String xpath, String strText, String strLink, int colVal)
    {
    WebElement tblData = driver.findElement(By.xpath(xpath));

    List tblRows = tblData.findElements(By.tagName("tr"));

    for (WebElement tblRow: tblRows)
    {
    List tblCols = tblRow.findElements(By.tagName("td"));

    for (WebElement tblCol: tblCols)
    {
    if(tblCol.getText().equals(strText))
    {
    List actions = tblCols.get(colVal).findElements(By.tagName(strLink));
    actions.get(0).click();
    }
    }
    }
    }
    }

    ReplyDelete
  2. Good code! Thank you!
    I would add only generic type to all List interfaces

    ReplyDelete
  3. @mohan, @VR: nice code. Thankyou

    ReplyDelete
  4. For above code ( Dynamic table) I am getting 'java.lang.NullPointerException'

    Pl do let me know how to correct the issue.

    Thanks
    Aniruddha

    ReplyDelete
    Replies
    1. @aniruddha can you please share the code and the error.

      Thanks for such a nice post. I was able to run through the program without any errors

      Delete
  5. hi i need help in retrieving dynamic data from datatable in retrieving each element

    ReplyDelete
  6. How could this be done in VBA/Excel?

    ReplyDelete