Sunday 25 June 2017

Selenium 3 – How to read dynamic data from WebTable and find highest value from a column



 how to get table data in selenium webdriver, how to handle dynamic elements in selenium webdriver, how to handle dynamic xpath in selenium webdriver, selenium iterate through table rows


Selenium 3 – How to read dynamic data from WebTable and find highest value from a column


WebTable Used:TOP GAINERS




CODE:

//Selenium 3- Reading and printing the values from dynamic web table and finding the highest value from a  particular column


package p1.com;

import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;


public class WebTableEg {

          //Driver initialization
              public static WebDriver drv=null;

              //WebTable Xpath Value
              String rfirst=".//*[@id='keymatgtb1']/table/tbody/tr[";
              String rthree="]/td[";
              String rfourth="]";

              //Xpath Variable initialization
              String path= null;


              @BeforeMethod
              public void beforeMethod() {

                     System.setProperty("webdriver.gecko.driver","C:\\Users\\anusha1983\\Downloads\\geckodriver-v0.17.0-win64\\geckodriver.exe");

                     DesiredCapabilities capabilities = DesiredCapabilities.firefox();
                     capabilities.setCapability("marionette", true);
                     drv = new FirefoxDriver(capabilities);

                     drv.manage().window().maximize();
                     drv.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
                     drv.get("http://www.moneycontrol.com/");

              }

              @Test
              public void main() {
             WebTableEg eg = new WebTableEg();
                     eg.firstMethod();
                     eg.secondMethod();

              }

              @AfterMethod
              public void afterMethod() {
                     drv.quit();
              }

              public void firstMethod(){
                     // Row Count
                     System.out.println(drv);
                     int Row_count = drv.findElements(By.xpath(".//*[@id='keymatgtb1']/table/tbody/tr/td[4]")).size();

                     //Column Count
                     int Column_count = drv.findElements(By.xpath(".//*[@id='keymatgtb1']/table/tbody/tr")).size();

                     System.out.println("******************WebTable Values are: ");


                     String value = null;
                     for (int c = 1; c < Row_count; c++) {

                           for (int d=1; d<Column_count; d++){

                                  path= rfirst+c+rthree+d+rfourth;
                                  value = drv.findElement(By.xpath(path)).getText();
                                  System.out.print(value +" ");
                           }
                           System.out.println(" ");
                     }

                     System.out.println("******************\n ");

              }

              public void secondMethod(){
                     //Top %Gain values are stored in list
                     List<String> valueList = new ArrayList<String>();
                     System.out.println("***********%Gain Values are: ");

                     //Column Count
                     int Column_count = drv.findElements(By.xpath(".//*[@id='keymatgtb1']/table/tbody/tr")).size();

                     String path1 = null;

                     for(int i=1; i<=Column_count; i++){
                           path= rfirst+i+rthree+4+rfourth;
                           path1 = drv.findElement(By.xpath(path)).getText();
                           System.out.println(path1);
                           valueList.add(path1);
                           System.out.println("");             }
                     System.out.println("*****************\nTop %Gain:  " + Collections.max(valueList));

              }


}




OUTPUT:



























Selenium - Reading and printing the values from web table and finding the highest value from a particular Column
Note: Tweaking of Proxy is performed


package p1.com;

import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.openqa.selenium.firefox.FirefoxProfile;

public class WebTableNwPrg {

              //Driver initialization
              public static WebDriver drv=null;

              //WebTable Xpath Value
              String rfirst=".//*[@id='keymatgtb1']/table/tbody/tr[";
              String rthree="]/td[";
              String rfourth="]";

              //Xpath Variable initialzation
              String path= null;


              @BeforeMethod
              public void beforeMethod() {

                     //System.setProperty("webdriver.gecko.driver","C:\\Users\\geckodriver-v0.17.0-win64\\geckodriver.exe");

                     FirefoxProfile profile = new FirefoxProfile();
                     profile.setPreference("network.proxy.type", 4);
                     profile.setPreference("network.proxy.http", "localhost");
                     drv = new FirefoxDriver(profile);

                     drv.manage().window().maximize();
                     drv.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
                     drv.get("http://www.moneycontrol.com/");

              }

              @Test
              public void main() {
                     WebTableNwPrg eg = new WebTableNwPrg();
                     eg.firstMethod();
                     eg.secondMethod();

              }

              @AfterMethod
              public void afterMethod() {
                     drv.quit();
              }

              public void firstMethod(){
                     // Row Count
                     System.out.println(drv);
                     int Row_count = drv.findElements(By.xpath(".//*[@id='keymatgtb1']/table/tbody/tr/td[4]")).size();

                     //Column Count
                     int Column_count = drv.findElements(By.xpath(".//*[@id='keymatgtb1']/table/tbody/tr")).size();

                                  System.out.println("******************WebTable Values are: ");


                     String value = null;
                     for (int c = 1; c < Row_count; c++) {

                           for (int d=1; d<Column_count; d++){

                                  path= rfirst+c+rthree+d+rfourth;
                                  value = drv.findElement(By.xpath(path)).getText();
                                  System.out.print(value +" ");
                           }
                           System.out.println(" ");
                     }

                     System.out.println("******************\n ");

              }

              public void secondMethod(){
                     //Top %Gain values are stored in list
                     List<String> valueList = new ArrayList<String>();
                     System.out.println("***********%Gain Values are: ");
                     //Column Count
                     int Column_count = drv.findElements(By.xpath(".//*[@id='keymatgtb1']/table/tbody/tr")).size();

                     String path1 = null;

                     for(int i=1; i<=Column_count; i++){
                           path= rfirst+i+rthree+4+rfourth;
                           path1 = drv.findElement(By.xpath(path)).getText();
                           System.out.println(path1);
                           valueList.add(path1);
                           System.out.println("");             }
                     System.out.println("*****************\nTop %Gain:  " + Collections.max(valueList));

              }

       }


OutPut:








No comments:

Post a Comment