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();
Good explanation...thank you
ReplyDeleteEven you can use Iterator to iterate through the rows and columns of the table.
below code works as yours.
we=wd.findElement(By.id("allpage_links"));
System.out.println("Table nameis"+we.getText());
System.out.println();
List rows=we.findElements(By.tagName("tr"));
Iterator i=rows.iterator();
while(i.hasNext())
{
List col=((WebElement)i.next()).findElements(By.tagName("tr"));
Iterator c=col.iterator();
while(i.hasNext())
{ System.out.print(((WebElement)i.next()).getText()+" ");
}
}
I didnt understand the for loop part
ReplyDelete