Handling Dynamic Web Tables Using Selenium WebDriver

There are two types of HTML tables published on the web-
  1. Static tables: Data is static i.e. Number of rows and columns are fixed.
  2. Dynamic tables: Data is dynamic i.e. Number of rows and columns are NOT

  1. fixed.Handling static table is easy, but dynamic table is a little bit difficult as rows and columns are not constant.

Using X-Path to Locate Web Table Elements

Before we locate web element, first let's understands-
What is a web element?
Web elements are nothing but HTML elements like textbox, dropdowns radio buttons, submit buttons, etc. These HTML elements are written with start tag and ends with an end tag.
For Example,
<p> My First HTML Document</p>.
Steps for getting X-path of web element that we want to locate.
Step 2) Right click on web element whose x-path is to be fetched. In our case, right click on "Company" Select Inspect option. The following screen will be shown -

Step 3) Right Click on highlighted web element > Select Copy -> Copy x-path option.

Step 4) Use the copied X-path "//*[@id="leftcontainer"]/table/thead/tr/th [1]" in Selenium WebDriver to locate the element.

Example: Fetch number of rows and columns from Dynamic WebTable

When the table is dynamic in nature, we cannot predict its number of rows and columns.
Using Selenium web driver, we can find
  • Number of Rows and columns of web table
  • X row or Y column's data.
Below is program for fetching total number of rows and columns of web table
ScreenShot
import java.text.ParseException;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.chrome.ChromeDriver;
public class Noofrowsandcols {
    public static void main(string[] args) throws ParseException {
        WebDriver wd;
        System.setProperty("webdriver.chrome.driver", "D:\\ chromedriver.exe") ;
        wd = new ChromeDriver();
        wd.get("http://money.rediff.com/gainers/bsc/dailygroupa?");         
        //No.of Columns
        List <WebElement> col = wd.findElements(By.xpath(".//*@id="leftcontainer']/table/thead/tr/th"));
        System.out.println("No of cols are : " +col.size()); 
        //No.of rows 
        List <WebElement> rows = wd.findElements(By.xpath(".//*[@id="leftcontainer']/table/tbody/tr/td[1]")); 
        System.out.println("No of rows are : " + rows.size());
        wd.close();
    }
Code Explanation:
  • Here we have first declared Web Driver object "wd" &initialized it to chrome driver.
  • We use List <WebElement> to total number of columns in "col".
  • findElements commands returns a list of ALL the elements matching the specified locator
  • using findElements and X-path //*@id="leftcontainer']/table/thead/tr/th")); we get all the columns
  • Similarly, we repeat the process for rows.
.Output:

Example: Fetch cell value of a particular row and column of the Dynamic Table

Comments

  1. Wonderful Blog on recent updates, I have updated my knowledge through your blog, Thanks admin for sharing the recent information.
    testing training chennai
    Software testing institutes in chennai

    ReplyDelete
  2. I have visited this blog first time and i got a lot of informative data from here which is quiet helpful for me indeed. 

    java training in tambaram | java training in velachery

    java training in omr | oracle training in chennai

    java training in annanagar | java training in chennai

    ReplyDelete
  3. Thank you for this post. Thats all I are able to say. You most absolutely have built this blog website into something speciel. You clearly know what you are working on, youve insured so many corners.thanks
    python training in pune
    python online training
    python training in OMR

    ReplyDelete
  4. Thanks for splitting your comprehension with us. It’s really useful to me & I hope it helps the people who in need of this vital information. 
    Blueprism training institute in Chennai

    Blueprism online training

    Blue Prism Training Course in Pune

    Blue Prism Training Institute in Bangalore

    ReplyDelete
  5. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
    angularjs-Training in sholinganallur

    angularjs-Training in velachery

    angularjs Training in bangalore

    angularjs Training in bangalore

    angularjs Training in btm

    ReplyDelete
  6. Greetings. I know this is somewhat off-topic, but I was wondering if you knew where I could get a captcha plugin for my comment form? I’m using the same blog platform like yours, and I’m having difficulty finding one? Thanks a lot.
    AWS Interview Questions And Answers
    AWS Online Training | Online AWS Certification Course - Gangboard
    AWS Training in Chennai | AWS Training Institute in Chennai Velachery, Tambaram, OMR
    AWS Training in Bangalore |Best AWS Training Institute in BTM ,Marathahalli

    ReplyDelete

Post a Comment

Popular posts from this blog

Verify Specific Position of an Element

Read it out for TESTNG before going for an iterview