How to Handle Dynamic Web Table in Selenium WebDriver

In this post, we will learn how to handle dynamic web table with Selenium Webdriver.

1. What is Dynamic Web Table?

First of all let's understand that what we call a dynamic table. So here is the answer, there might occur some scenarios where every time a page loads with different number of rows and columns or different columns for each row. When we don't know that how the expected data outcome will be presented in a table, tha't's called a dynamic table.

2. How to Handle Dynamic Table

Selenium doesn't provide us any specific command for handling a dynamic table. This is a special case in automation, so to handle this case we have to write the logic inside our code to handle the dynamic loading of data. It will require both Selenium and Coding skills.

We can divide the solution of this problem statement into two parts:
  1. How to read data from Web Table with Selenium Webdriver?
  2. How to read data from Dynamic Web Table with Selenium Webdriver?

2.1 How to read data from Web Table with Selenium Webdriver

Following Selenium sample script explains how to extract data from table:

public class table {

 WebDriver driver = new FirefoxDriver();

 @BeforeTest
 public void setup() throws Exception {
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
  driver.get("https://www.techlistic.com/p/demo-selenium-practice.html");
 }

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

 @Test
 public void print_data(){
 //Get number of rows In table.
 int Row_count = driver.findElements(By.xpath("//*[@id='post']/div[1]/table/tbody/tr")).size();
 System.out.println("Number Of Rows = "+Row_count);

 //Get number of columns In table.
 int Col_count = driver.findElements(By.xpath("//*[@id='post']/div[1]/table/tbody/tr[1]/td")).size();
 System.out.println("Number Of Columns = "+Col_count);

 //divided xpath In three parts to pass Row_count and Col_count values.
 String first_part = "//*[@id='post']/div[1]/table/tbody/tr[";
 String second_part = "]/td[";
 String third_part = "]";

 //Used for loop for number of rows.
 for (int i=1; i<=Row_count; i++){
  
  //Used for loop for number of columns.
  for(int j=1; j<=Col_count; j++){
   
   //Prepared final xpath of specific cell as per values of i and j.
   String final_xpath = first_part+i+second_part+j+third_part;
   
   //Will retrieve value from located cell and print It.
   String Table_data = driver.findElement(By.xpath(final_xpath)).getText();
   System.out.print(Table_data +" ");
  }

  System.out.println("");
  System.out.println("");
  }
 }
}

Console Output:

Number Of Rows = 3
Number Of Columns = 6

11  12  13  14  15  16  

21  22  23  24  25  26  

31  32  33  34  35  36

2.2 How to Handle Dynamic Web Table with Selenium

For that, we have to put two for loops,

1. First loop will iterate all the rows.
2. Second loop inside it, will iterate the columns of each row.


public class DynamicTable {

  WebDriver driver = new FirefoxDriver();
  
  @BeforeTest
  public void setup() throws Exception {
  driver.manage().window().maximize();

 
 driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

  driver.get("https://www.techlistic.com/p/demo-selenium-practice.html");
  }

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

  @Test
  public void test_Dynamic_Webtable() {
  // Locate table
  WebElement mytable = driver.findElement(By.xpath(".//*[@id='post']/div[1]/table/tbody"));

  // Locate rows of table and save locators of each row in a list
  List rows_table = mytable.findElements(By.tagName("tr"));

  // Get 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++) 
  {
    // locate columns(cells) of that specific row.
    List Columns_row = rows_table.get(row).findElements(By.tagName("td"));

    // Get 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++) {

  // 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);
   }
  }
 }
}


How to find Broken Links with Selenium  << Previous   ||   Next >>   How to Upload File with Selenium

Follow Us

Quora Space | Facebook Page | Telegram Channel
Feel free to ask queries or share your thoughts in comments or email us.

Comments

Popular posts from this blog

10 Best Demo Websites for Selenium Automation Testing Practice

Automate an e-Commerce website with Selenium Webdriver

Top 50 Selenium Webdriver Interview Questions

Automate Browser Actions and Menu links with Selenium Webdriver

Top 25 Selenium Webdriver Commands for Test Automation