How to retrieve number of rows, columns and data in cells in a table in Selenium Webdriver
- qatestingthreesixt
- Dec 1, 2021
- 1 min read
driver.get("http://money.rediff.com/”");
WebElement element = driver.findElement(By.id("allpage_links"));
List < WebElement > rowC = element.findElements(By.xpath("*[@id=’allpage_links’]/tbody/tr"));
System.out.println("Number of rows in this table: "+rowC.size());
//Here i_RowN and i_ColN, Using to indicate Row and Column numbers.
int i_RowN = 1;
for (WebElement rowElement: rowC) {
List < WebElement > colC = rowElement.findElements(By.xpath("td"));
int i_ColN = 1;
for (WebElement colE: colC) {
System.out.println("Row" + i_RowN + "Column" + i_ColN + "Data" + colE.getText());
i_ColN = i_ColN + 1;
}
i_RowN = i_RowN + 1;
}
driver.close();
//To Retrieve text from particular cell, please use following code:
int rownum, colnum; String s_xpath;
driver.get("http://money.rediff.com/”");
rownum = 2; colnum = 1;
//Here i am framing the xpath with rownum and colnum
s_xpath = "//*[@id=’allpage_links’]/tbody/tr["+rownum+"]/td["+colnum+"]";
//getText method retrieves the cell value.
System.out.println(driver.findElement(By.xpath(s_xpath)).getText());
Opmerkingen