This below code with comments demonstrates how we can work with web tables in selenium.
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import java.util.List;
public class HerokuAppWebTable {
@FindBy(xpath = ".//*[@id='table1']/thead/tr") private static WebElement tblHeader;
@FindBy(xpath = ".//*[@id='table1']/tbody") private static WebElement tblBody;
// @FindBy(xpath = ".//*[@id='table1']/tbody") private static WebElement tblBody;
// this the webtable from the elemental selenium example. this is a simple td/tr type of table, and not one with DIV tags
// the body of the table [without its headers] can be located by this xpath = ".//*[@id='table1']/tbody"
// if we do a getText on this element, we can get the text of the entire table, without the headers
// the column headers are not part of the table body, hence, the number of rows returned by this method is the actual number of data records present in the table
// the number of rows in this body of table can be located by this xpath = ".//*[@id='table1']/tbody/tr"
// to get the number of rows under the body of the table, use a combination of List <WebElement> and findElementsBy methods
// So, in short, to work with web tables, locate the table body [by building xpath till tbody element], then get the data rows [by locating all elements with tr tag]
// There is no direct way of working with web tables in selenium, no WebTable element!
// There is another method to get the number of rows in the table as shown below
// List<WebElement> rowNum = driver.findElements(By.xpath(".//*[@id='table1']/tbody/tr"));
// System.out.println("Row Num = " + rowNum.size());
// here we have specified the xpath till the row elements, without creating a table element first
public HerokuAppWebTable(){}
public void actionTable(){
System.out.println("Entire text of the table is as below");
String tableText = tblBody.getText();
// System.out.println(tableText);
List <WebElement> rows = tblBody.findElements(By.tagName("tr"));
System.out.println("Number of rows in table = " + rows.size());
// List <WebElement> cols = tblHeader.findElements(By.tagName("th"));
// System.out.println("Number of columns in the table =" + cols.size());
// findElements method returns WebElement of the type List, hence, we have to always declare List<WebElement> when using the findElements method.
int rowCount = rows.size();
// looping table data row by row
for (int i = 0; i < rowCount; i++) {
// locating all the columns of this row
// we are not writing this step to get the count of columns [although that will be needed] but more so to get the child <td>
// element present under the partent <tr> element of the row
// this <td> element is the one that contains the data for that column [under the row that we are looping for]
// so, we dont need to find the count of headers at the start, we are doing that coincidentally at this step
List<WebElement> colRow = rows.get(i).findElements(By.tagName("td"));
int colCount = colRow.size();
// looping to get data for all the columns in this row
for (int j = 0; j < colCount; j++) {
String cellText = colRow.get(j).getText();
System.out.println("cell text = " + cellText);
} //col loop
} //row loop
// if we know the cell from which we want to capture the data, we can write something like below
// getting data for the 3rd row and 2nd column - remember the index starts with 0!
List<WebElement> dataRow = rows.get(2).findElements(By.tagName("td"));
String cellValue = dataRow.get(1).getText();
// and if we want to click on the link in the 6th col
dataRow.get(5).click();
} //end of method
} //end of class
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import java.util.List;
public class HerokuAppWebTable {
@FindBy(xpath = ".//*[@id='table1']/thead/tr") private static WebElement tblHeader;
@FindBy(xpath = ".//*[@id='table1']/tbody") private static WebElement tblBody;
// @FindBy(xpath = ".//*[@id='table1']/tbody") private static WebElement tblBody;
// this the webtable from the elemental selenium example. this is a simple td/tr type of table, and not one with DIV tags
// the body of the table [without its headers] can be located by this xpath = ".//*[@id='table1']/tbody"
// if we do a getText on this element, we can get the text of the entire table, without the headers
// the column headers are not part of the table body, hence, the number of rows returned by this method is the actual number of data records present in the table
// the number of rows in this body of table can be located by this xpath = ".//*[@id='table1']/tbody/tr"
// to get the number of rows under the body of the table, use a combination of List <WebElement> and findElementsBy methods
// So, in short, to work with web tables, locate the table body [by building xpath till tbody element], then get the data rows [by locating all elements with tr tag]
// There is no direct way of working with web tables in selenium, no WebTable element!
// There is another method to get the number of rows in the table as shown below
// List<WebElement> rowNum = driver.findElements(By.xpath(".//*[@id='table1']/tbody/tr"));
// System.out.println("Row Num = " + rowNum.size());
// here we have specified the xpath till the row elements, without creating a table element first
public HerokuAppWebTable(){}
public void actionTable(){
System.out.println("Entire text of the table is as below");
String tableText = tblBody.getText();
// System.out.println(tableText);
List <WebElement> rows = tblBody.findElements(By.tagName("tr"));
System.out.println("Number of rows in table = " + rows.size());
// List <WebElement> cols = tblHeader.findElements(By.tagName("th"));
// System.out.println("Number of columns in the table =" + cols.size());
// findElements method returns WebElement of the type List, hence, we have to always declare List<WebElement> when using the findElements method.
int rowCount = rows.size();
// looping table data row by row
for (int i = 0; i < rowCount; i++) {
// locating all the columns of this row
// we are not writing this step to get the count of columns [although that will be needed] but more so to get the child <td>
// element present under the partent <tr> element of the row
// this <td> element is the one that contains the data for that column [under the row that we are looping for]
// so, we dont need to find the count of headers at the start, we are doing that coincidentally at this step
List<WebElement> colRow = rows.get(i).findElements(By.tagName("td"));
int colCount = colRow.size();
// looping to get data for all the columns in this row
for (int j = 0; j < colCount; j++) {
String cellText = colRow.get(j).getText();
System.out.println("cell text = " + cellText);
} //col loop
} //row loop
// if we know the cell from which we want to capture the data, we can write something like below
// getting data for the 3rd row and 2nd column - remember the index starts with 0!
List<WebElement> dataRow = rows.get(2).findElements(By.tagName("td"));
String cellValue = dataRow.get(1).getText();
// and if we want to click on the link in the 6th col
dataRow.get(5).click();
} //end of method
} //end of class
No comments:
Post a Comment