If I have a table which is designed in this manner:
If you have an ID for the table its really good to catch the table else you will have to do alot of manipulation with the tables you find...
// Here I try to identify the table using its ID.
WebElement table = webdriver.findElement(By.id("tableID"));
// Here I try to identify the table using a simple xpath
WebElement table = webdriver.findElement(By.xpath("/html/body/table/tbody"));
// Get all the rows of the table
List rows = table.findElements(By.tagName("tr"));
Iterator i = rows.iterator();
System.out.println("-----------------------------------------------");
// Iterate over the rows of the table
while(i.hasNext()) {
WebElement row = i.next();
// Find all the columns of the row
List columns = row.findElements(By.tagName("td"));
Iterator j = columns.iterator();
System.out.print(" | ");
// Iterate over the columns of the particular row
while(j.hasNext()) {
WebElement column = j.next();
System.out.print(column.getText());
System.out.print(" | ");
}
System.out.println("");
System.out.println("-----------------------------------------------");
}
In Selenium1:
You can use, getTable() function
If you have an ID for the table its really good to catch the table else you will have to do alot of manipulation with the tables you find...
// Here I try to identify the table using its ID.
WebElement table = webdriver.findElement(By.id("tableID"));
// Here I try to identify the table using a simple xpath
WebElement table = webdriver.findElement(By.xpath("/html/body/table/tbody"));
// Get all the rows of the table
List
Iterator
System.out.println("-----------------------------------------------");
// Iterate over the rows of the table
while(i.hasNext()) {
WebElement row = i.next();
// Find all the columns of the row
List
Iterator
System.out.print(" | ");
// Iterate over the columns of the particular row
while(j.hasNext()) {
WebElement column = j.next();
System.out.print(column.getText());
System.out.print(" | ");
}
System.out.println("");
System.out.println("-----------------------------------------------");
}
In Selenium1:
You can use, getTable() function
Comments