dynamic table data retrieve - selenium webdriver
Solution 1
Try below code, this will print all cells data,
// Grab the table
WebElement table = driver.findElement(By.id("divListView"));
// Now get all the TR elements from the table
List<WebElement> allRows = table.findElements(By.tagName("tr"));
// And iterate over them, getting the cells
for (WebElement row : allRows) {
List<WebElement> cells = row.findElements(By.tagName("td"));
// Print the contents of each cell
for (WebElement cell : cells) {
System.out.println(cell.getText());
}
}
Solution 2
// Grab the table
WebElement table = driver.findElement(By.id("table-6"));
//Get number of rows in table
int numOfRow = table.findElements(By.tagName("tr")).size();
//Get number of columns In table.
int numOfCol = driver.findElements(By.xpath("//*[@id='table-6']/tbody/tr[1]/td")).size();
//divided Xpath In three parts to pass Row_count and Col_count values.
String first_part = "//*[@id='table-6']/tbody/tr[";
String second_part = "]/td[";
String third_part = "]";
//take the second column values
int j=2;
//List to store the second column
List<String> secondColumnList=new ArrayList<String>();
//Loop through the rows and get the second column and put it in a list
for (int i=1; i<=numOfRow; i++){
//Prepared final xpath of specific cell as per values of i and j.
String final_xpath = first_part+i+second_part+j+third_part;
//Will retrieve value from located cell and print It.
String test_name = driver.findElement(By.xpath(final_xpath)).getText();
secondColumnList.add(test_name);
System.out.println(test_name);
}
Solution 3
Dynamic table data capturing:
1.First of all Capture Table Head Count.
[int tHeadCount = driver.findElements(By.xpath("//table//tr//th")).size();
]
2.Capture Table Row Count in which row your actual data exists. [-in my point of view i need data from first row it self, so i am hard coding it to zero.] If you want please add one for loop to existing code.
3.The actual solution starts from here. Following is function call "Deposited By" is table heading text of corresponding table data.
String tableDataValue = managePackageTableData("Deposited By");
public String managePackageTableData(String columnName) {
//In Following line i am capturing table contains how many headers.
int tHeadCount = driver.findElements(By.xpath("//table//tr//th")).size();
int statusIndex = 0;
for(int i=0;i<tHeadCount-1;i++)
{
String theadValue = driver.findElements(By.className("table")).get(0).findElements(By.tagName("tr")).get(0).findElements(By.tagName("th")).get(i).getText();
if(theadValue.equalsIgnoreCase(columnName))
{
statusIndex = i;
break;
}
}
String tableData = driver.findElements(By.tagName("tbody")).get(0).findElements(By.tagName("tr")).get(0).findElements(By.tagName("td")).get(statusIndex).getText();
return tableData;
}
Prabu
Automation Test Engineer Selenium WebDriver Katalon Studio Java / Groovy
Updated on July 09, 2022Comments
-
Prabu almost 2 years
<table id="tblListViewHeader" class="adminlist" cellspacing="1" cellpadding="0" style="table-layout: fixed; width: 1003px;"> <tbody> </table> </td> </tr> <tr> <td> <div id="divListView" style="width: 100%; height: 300px; overflow: auto; display: block;"> <table id="tblListView" class="adminlist" cellspacing="1" cellpadding="0" style="table-layout: fixed; width: 100%;"> <tbody data-bind="template: { name: 'ActiveGradeTemplate', foreach: ActiveGrade }"> <tr class="row0"> <td data-bind="text:$index()+1" style="width: 5%;">1</td> <td data-bind="text: GradeName" style="width: 20%;">Vantage Point</td> <td align="right" data-bind="text: DisplayCreatedDate" style="width: 10%;">27 Mar 2013</td> <td align="right" data-bind="text: CreatedByUser" style="width: 10%;">Name</td> <td align="right" data-bind="text: DisplayModifiedDate" style="width: 10%;">27 Mar 2013</td> <td align="right" data-bind="text: ModifiedByUser" style="width: 10%;">Name</td> <td align="center" data-bind="text: Status" style="width: 5%;">Active</td> <td align="center" style="width: 10%;"> <a id="lnkEdit_7" data-bind="click: $root.lnkEdit, attr:{'id':'lnkEdit_' + GradeID}" href="#">Edit</a> <span id="spanEdit_7" data-bind="attr:{'id':'spanEdit_' + GradeID}"></span> </td> </tr> <tr class="row0"> <td data-bind="text:$index()+1" style="width: 5%;">2</td> <td data-bind="text: GradeName" style="width: 20%;">test grade</td> <td align="right" data-bind="text: DisplayCreatedDate" style="width: 10%;">Yesterday</td> <td align="right" data-bind="text: CreatedByUser" style="width: 10%;">Name</td> <td align="right" data-bind="text: DisplayModifiedDate" style="width: 10%;">Yesterday</td> <td align="right" data-bind="text: ModifiedByUser" style="width: 10%;">Name</td> <td align="center" data-bind="text: Status" style="width: 5%;">Active</td> <td align="center" style="width: 10%;"> <a id="lnkEdit_11" data-bind="click: $root.lnkEdit, attr:{'id':'lnkEdit_' + GradeID}" href="#">Edit</a> <span id="spanEdit_11" data-bind="attr:{'id':'spanEdit_' + GradeID}"></span> </td> </tr>
How can I retrieve the
td
values for each and every row, this is for Dynamic generation. All thetr
class names are the same:<tr class="row0">
. How do I retreive the table data for the above formatted table?