Wednesday, 30 December 2015

Get or Print the web table data

package interview;

import java.util.List;

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

public class WebTable_3 {
static WebDriver driver;
   
    public static void main(String[] args) throws InterruptedException
    {
       
         driver=new FirefoxDriver();
    driver.manage().window().maximize();
    driver.get("http://money.rediff.com/");
           
    WebElement element=driver.findElement(By.id("allpage_links"));
    Thread.sleep(5000);
    List<WebElement> rowCollection=element.findElements(By.xpath("//*[@id='allpage_links']/tbody/tr"));


    System.out.println("Numer of rows in this table: "+rowCollection.size());
    //Here i_RowNum and i_ColNum, i am using to indicate Row and Column numbers. It may or may not be required in real-time Test Cases.      
    int i_RowNum=1;
    for(WebElement rowElement:rowCollection)
    {
          List<WebElement> colCollection=rowElement.findElements(By.xpath("td"));
          int i_ColNum=1;
          for(WebElement colElement:colCollection)
          {
               System.out.println("Row "+i_RowNum+" Column "+i_ColNum+" Data "+colElement.getText());
               i_ColNum=i_ColNum+1;
           }
        i_RowNum=i_RowNum+1;
     }
    }
}

=========other way ==========
//Printing the webTable content with using iterator
package interview;

import java.util.Iterator;
import java.util.List;

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

public class Print_WebTableContent_With_Iterator {

    static WebDriver driver;
    public static void main(String[] args) {
    driver=new FirefoxDriver();
    driver.get("http://www.w3schools.com/html/html_tables.asp");
    System.out.println("Browser Opened:");
    List<WebElement> we= driver.findElements(By.xpath("//*[@id='main']/table[1]"));
   
    Iterator<WebElement> itr=we.iterator();
    while(itr.hasNext())
    {
        System.out.println(itr.next().getText());
    }
       
   
    }

}

==========Other way+++++++++++
//Runable code to read data from web table
package interview;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;

public class Webtable_4_fb_run {

public static void main(String[] args) {
ProfilesIni pr = new ProfilesIni();
FirefoxProfile fp = pr.getProfile("FirefoxProfile_Selenium");
FirefoxDriver driver = new FirefoxDriver(fp);

driver.get("http://www.timeanddate.com/worldclock/");
driver.manage().window().maximize();

WebElement table = driver.findElement(By.xpath("html/body/div[1]/div[7]/section[2]/div[1]/table"));
List<WebElement> rows = table.findElements(By.tagName("tr"));

for(int i = 0; i<= rows.size(); i++)
{
List<WebElement> cols = rows.get(i).findElements(By.tagName("td"));

for(int j = 0; j < cols.size();j++)
{
System.out.print(cols.get(j).getText() + "---------");
}

System.out.println();
}

}

}

++++++++++++++++++++++++++++++++++++++++++
 package Other;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class Table_Data_Test {
  public String baseURL = "http://accessibility.psu.edu/tablecomplexhtml";
  public WebDriver driver = new FirefoxDriver();

  @BeforeMethod
  public void setup()
  {
   driver.get(baseURL);
  }

  @AfterMethod
  public void tear()
  {
   driver.quit();
  }

  @Test
  public void testTableExample()
  {
   List<WebElement> rows = driver.findElements(By.xpath(".//*[@id='node-281']/div/div[1]/div[2]/table/tbody/tr")); // Returns the number of Rows
   List<WebElement> cols = driver.findElements(By.xpath(".//*[@id='node-281']/div/div[1]/div[2]/table/tbody/tr[1]/td")); // Returns the number of Columns
 
   // Static XPath of a single TR and TD is ".//*[@id='node-281']/div/div[1]/div[2]/table/tbody/tr[1]/td[1]"
 
   // code to fetch each and every value of a Table
 
   for(int rowNumber = 1; rowNumber <= rows.size(); rowNumber++)
   {
    for(int colNumber = 1; colNumber <= cols.size(); colNumber++)
    {
     System.out.print(driver.findElement(By.xpath(".//*[@id='node-281']/div/div[1]/div[2]/table/tbody/tr["+rowNumber+"]/td["+colNumber+"]")).getText()+"  ");
    }
    System.out.println("\n");
   }
 
   System.out.println("###############################################################################\n");

  // nth element value of a table
 
   System.out.println(driver.findElement(By.xpath(".//*[@id='node-281']/div/div[1]/div[2]/table/tbody/tr["+rows.size()+"]/td["+cols.size()+"]")).getText());
 
  }
}

No comments:

Post a Comment