Address.212, 2nd Floor, Grand Mall, Jalandhar, Punjab 144001, India

Tel. +91-7710795555

      +91-7710793555

QUICK LINKS

© 2019 by NTF. Proudly created with 9to5Technologies  privacy policy

Search
  • 9to5 Technologies

How to retrieve number of rows, columns and data in cells in a table in Selenium Webdriver

driver.get("http://money.rediff.com/&#8221");

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/&#8221;");

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());

1 view