Showing posts with label Web Table. Show all posts
Showing posts with label Web Table. Show all posts

Friday, October 26, 2012

How to retrieve web table content using Selenium WebDriver

There are many ways we can do this,
but in below example i am following "tr","td" approach.

WebDriver driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://money.rediff.com/");
       
WebElement element=driver.findElement(By.id("allpage_links"));
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;
 }
 driver.close();