Tutorials Hut

  • Selenium WebDriver

       Selenium Introduction
       Benefits of Selenium
       Four components of Selenium
       Difference b/w Selenium IDE, RC & WebDriver
       Selenium WebDriver Architecture
       Background when user execute selenium code
       Download and Install Java
       Download and Install Eclipse
       Download Selenium WebDriver
       Selenium WebDriver Locators
       Selenium - Launch Browser
       Selenium WebDriver Waits
       Selenium- Implicit wait
       Selenium- Explicit wait
       Selenium- Fluent wait
       Selenium- Commonly used commands
       Selenium- findElement & findElements
       Selenium- Selenium-Handling check Box
       Selenium- Handling Radio button
       Selenium- Handling drop down
       Selenium- Take Screenshot
       Selenium- Handle Web Alerts
       Selenium- Multiple Windows Handling
       Selenium- Handle iframes
       Selenium- Upload a file
       Selenium- Download a file
       Selenium- Actions Class Utilities
       Selenium- Mouse Actions
       Selenium- Keyboards Events
       Selenium- Handle mouse hover Actions
       Selenium- Drag and Drop
       Selenium- Scroll a WebPage
       Selenium- Context Click / Right Click
       Selenium- Double Click
       Selenium- Desired Capabilities
       Selenium- Assertions
       Selenium- Exceptions and Exception Handling
       Selenium- Difference b/w driver.close() & driver.quit()
       Selenium- difference b/w driver.get() & driver.navigate()
       Selenium- JavascriptExecutor
       Selenium- Read excel file using Fillo API
       Selenium- Database Testing using Selenium
       Selenium- Read & write excel file using Apache POI
       Selenium- Read and Write csv file in Selenium
       Selenium- Dynamic Web Table Handling
       Selenium- Maven Integration with Selenium
       Selenium- Set up Logging using Log4j
       Selenium-Implement Extent Report



  • Dynamic Web Table Handling using Selenium WebDriver

    This article will present you with a complete idea about the testing of web tables and how to handle web tables using selenium webdriver. 

    However, let’s begin with understanding the types of web tables, and how they work.

    Dynamic Web Table Handling using Selenium WebDriver_1

    We will learn below topics in this article

      1. What is WebTable in selenium?
      2. Types of Web Tables
      3. Handling of WebTable using selenium Webdriver

    What is WebTable in selenium?

    Web table in Selenium is a WebElement like any other WebElements for example text boxes, checkboxes, drop-down etc. 

    Web tables are a group of elements that are stored in a row and column format

    The table created for a web page is called a web table. 

    Below are some of the important tags associated with a web table:

    < table > – Defines an HTML table

    < th > -defines header information in a table

    < tr > – defines a row in a table

    < td > – defines a column in a table

    Example:

    Below is the sample html structure of the html table.


















    First NameLast NameCity
    PaulJonesLondon
    SteveSmithSydney
    TraceySmithNew york
     

    so it looks like in below format.

    Dynamic Web Table Handling using Selenium WebDriver_2

    Types of Web Tables

    Web Tables are categorized into two types which  are Static Web Tables and Dynamic Web Tables.

      1. Static web table:  The Number of rows and columns will be static. 
      2. Dynamic web table: Number of rows and columns will be dynamically changing, the information displayed in the table is dynamic.  it keeps on increasing or decreasing based on data for e.g. sales reports, customer data etc.

    It is easy to handle static web tables, but handling dynamic web tables is a little bit difficult as rows and columns are dynamic in nature and always keep changing.

    Handling of WebTable using selenium Webdriver

    Below are few operations a tester do during automation of a webtable

    • Locate Web Table Elements using X-Path 

    • Find the number of rows and columns  the dynamic web table using selenium

    • Find cell value for specific row & column of the webtable

    Let’s understand the handling of web tables with the help of an example. below is the data for top gainers in nifty 50 stocks on nse website

    https://www.nseindia.com/market-data/top-gainers-loosers?cat=G

    Dynamic Web Table Handling using Selenium WebDriver_3

    Locate Web Table Elements using X-Path 

    Let’s select an element in the web table and find its XPath.

    For Chrome, right-click and inspect the given element to find its XPath.

    In this example we will find xpath of Company ‘TATAMOTORS’ 

      • Select element which needs to we identify in a webtable
      • Right click and click on inspect
      • Copy xpath as per below screenshot
      • So here Xpath will be -//*[@id=”topgainer-Table”]/tbody/tr[9]/td[1]/a



    Dynamic Web Table Handling using Selenium WebDriver_4

    Let’s assume if position of TATAMOTORS is not fixed in a webTable or it is a dynamic table then above xpath may fail

    so for this we will write xpath by own , so we can write xpath for ‘TATAMOTORS’  as

    //td/*[text()=’TATAMOTORS’]

    Find the number of rows and Columns the dynamic web table using selenium

     

    Below is the code snippet to find the total number of rows and columns for the above Dynamic Web Table

    //Finding number of Rows

    //Find all the webelements with Tag name ‘tr’ in the web table

    List<WebElement> rowsNumber = driver.findElements(By.xpath("//table[@id='topgainer-Table']/tbody/tr"));
    int rowCount = rowsNumber.size();
    system.out.println("No of rows in this table : " + rowCount);

    //Finding number of Columns

    List<WebElement> columnsNumber = driver.findElements(By.xpath("//table[@id='topgainer-Table']/thead/tr/th"));
    int columnCount = columnsNumber.size();
    system.out.println("No of columns in this table : " + columnCount);

    Find cell value for specific row & column of the webtable

    Let’s take an example and we need to fetch third of the table and its fourth cell’s data of the table below

      • Open Google chrome browser.
      • Open URL: https://www.nseindia.com/market-data/top-gainers-loosers?cat=G
      • Find the Third row of a table
      • Find the fourth cell of the third row and print the value
      • Close the driver

    //Code Snippet

    package com.test;
     
    import java.util.concurrent.TimeUnit;
     
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
     
    import io.github.bonigarcia.wdm.WebDriverManager;
     
    public class WebTableTest {
     
    	public static void main(String[] args) {
     
    		WebDriverManager.chromedriver().setup();
            WebDriver driver = new ChromeDriver();
     
            //open the UTRL
    		driver.get("https://www.nseindia.com/market-data/top-gainers-loosers?cat=G");
    		driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
     
    		//identify the webTable
    		WebElement table = driver.findElement(By.tagName("table"));
     
    		// To find the third row of a table
    		WebElement Rowtable = table.findElement(By.xpath("//table[@id='topgainer-Table']/tbody/tr[3]"));
    		String rowtext = Rowtable.getText();
    		System.out.println("The row text of the web table : " + rowtext);
     
    		// To get third row and fourth Column
    		WebElement cellNo = Rowtable.findElement(By.xpath("//table[@id='topgainer-Table']/tbody/tr[3]/td[4]"));
    		String valueNo = cellNo.getText();
    		System.out.println("Cell value is : " + valueNo);
    		driver.close();
     
    	}
     
    }
    

    Get Maximum of all the Values of a column in  Dynamic Table

    Let’s consider the below webtable and we have to find out the maximum ‘OPEN’ column value across all the given rows in a webtable.

    Dynamic Web Table Handling using Selenium WebDriver_5

    Scenario:

      1. Open Chrome Browser.
      2. Navigate to URL ‘https://www.nseindia.com/market-data/top-gainers-loosers?cat=G’
      3. Maximize the browser
      4. Find Largest value of column ‘OPEN’ in the webtable

    //Code Snippet

    package com.test;
     
    import java.text.NumberFormat;
    import java.text.ParseException;
    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.chrome.ChromeDriver;
     
    import io.github.bonigarcia.wdm.WebDriverManager;
     
    public class WebTableTest {
    //Get Maximum of all the Values in a Column 'Open' in Dynamic Table
    	
    	public static void main(String[] args) throws ParseException {
     
    		WebDriverManager.chromedriver().setup();
            WebDriver driver = new ChromeDriver();
     
            //open the UTRL
    		driver.get("https://www.nseindia.com/market-data/top-gainers-loosers?cat=G");
    		driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
     
    		driver.manage().window().maximize();
     
    		String maximum;
    	     double m=0,r=0;
     
    		 //No of Rows
    	     List<WebElement> rowsNumber = driver.findElements(By.xpath("//table[@id='topgainer-Table']/tbody/tr"));
    	     System.out.println("Total No of rows are : " + rowsNumber.size());
    	     
    	     //No. of Columns
    	     List<WebElement> columnsNumber = driver.findElements(By.xpath("//table[@id='topgainer-Table']/thead/tr/th"));
    	     int columnCount = columnsNumber.size();
    	     System.out.println("No of columns in this table : " + columnCount);
     
    	        //iterate over each row of the table
    	        for (int i =1;i<rowsNumber.size();i++)
    	        {    
    	        	//get Text of second column value of each row in the table
    	            maximum= driver.findElement(By.xpath("//table&#91;@id='topgainer-Table'&#93;/tbody/tr&#91;" + (i+1)+ "&#93;/td&#91;2&#93;")).getText();
    	            
    	            //Parse maximum String into the Double Value
    	            NumberFormat nformat =NumberFormat.getNumberInstance(); 
    	            Number num = nformat.parse(maximum);
    	            maximum = num.toString();
    	            m = Double.parseDouble(maximum);
    	            
    	            if(m>r)
    	             {    
    	                r=m;
    	             }
    	        }
    	        System.out.println("Maximum open price is : "+ r);
    	        driver.close();
       }
     
    }
    



  • Selenium WebDriver Tutorials

       Selenium Introduction
       Benefits of Selenium
       Four components of Selenium
       Difference b/w Selenium IDE, RC & WebDriver
       Selenium WebDriver Architecture
       Background when user execute selenium code
       Download and Install Java
       Download and Install Eclipse
       Download Selenium WebDriver
       Selenium WebDriver Locators
       Selenium - Launch Browser
       Selenium WebDriver Waits
       Selenium- Implicit wait
       Selenium- Explicit wait
       Selenium- Fluent wait
       Selenium- Commonly used commands
       Selenium- findElement & findElements
       Selenium- Selenium-Handling check Box
       Selenium- Handling Radio button
       Selenium- Handling drop down
       Selenium- Take Screenshot
       Selenium- Handle Web Alerts
       Selenium- Multiple Windows Handling
       Selenium- Handle iframes
       Selenium- Upload a file
       Selenium- Download a file
       Selenium- Actions Class Utilities
       Selenium- Mouse Actions
       Selenium- Keyboards Events
       Selenium- Handle mouse hover Actions
       Selenium- Drag and Drop
       Selenium- Scroll a WebPage
       Selenium- Context Click / Right Click
       Selenium- Double Click
       Selenium- Desired Capabilities
       Selenium- Assertions
       Selenium- Exceptions and Exception Handling
       Selenium- Difference b/w driver.close() & driver.quit()
       Selenium- difference b/w driver.get() & driver.navigate()
       Selenium- JavascriptExecutor
       Selenium- Read excel file using Fillo API
       Selenium- Database Testing using Selenium
       Selenium- Read & write excel file using Apache POI
       Selenium- Read and Write csv file in Selenium
       Selenium- Dynamic Web Table Handling
       Selenium- Maven Integration with Selenium
       Selenium- Set up Logging using Log4j
       Selenium-Implement Extent Report

















  • Leave a Reply

    Your email address will not be published. Required fields are marked *