Wednesday, January 14, 2015

Selenium script to get table values

package Ecomm_selenium;

import java.util.List;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Tablevalues {

       /**
       * @param args
       */
       public static void main(String[] args) {
              // TODO Auto-generated method stub
           //String paths = ".//*[@id='fk-mainbody-id']/div/div[2]";       
           //String
             
              WebDriver d = new FirefoxDriver();
              d.get(site);
              //WebElement webtable = d.findElement(By.xpath(".//*[@id='grey-box']/div"));
              int RowIndex = 3;
              List<WebElement> Rowcount = d.findElements(By.xpath(".//*[@id='grey-box']/div/div["+RowIndex+"]"));
             
              System.out.println("No: of Rows in table:" + Rowcount.size());
                    
              //for(WebElement rowelem : Rowcount)
              for(int i=0; i <= RowIndex; i++)
              {
                     int ColIndex = 3;
                     for(int j = 0; j <= ColIndex; j++){
                           WebElement value = d.findElement(By.xpath(".//*[@id='grey-box']/div/div["+i+"]/div["+j+"]/p"));
                           System.out.println("Row" + i + "Column" + j + "Data" + value.getText());
                     }
                    
                     
              }
              d.quit();
       }
      
}

Selenium script to get list of values

//code to get list of values

package Ecomm_selenium;
import java.util.List;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

//Getting list of webElements
public class Login {

                /**
                * @param args
                */
                public static void main(String[] args) {
                                // TODO Auto-generated method stub
                               
                                String site = "http://www.flipkart.com/";
                    String paths = ".//*[@id='fk-mainbody-id']/div/div[2]";                            
                    //String
                               
                                WebDriver d = new FirefoxDriver();
                                d.get(site);
                                List<WebElement> colu_vals = d.findElements(By.className("goquickly-list"));
                                java.util.Iterator<WebElement> i = colu_vals.iterator();
                                while(i.hasNext())
                                {
                                                WebElement value = i.next();
                                                System.out.println(value.getText());
                                               
                                }
                               
                               
                }

}