1.

How to get cell value from the table?

Answer»

There are TWO types of HTML tables on the web that are Static table and Dynamic table.

In the Static table, DATA is static ie.rows and columns are FIXED whereas in Dynamic table rows and columns are not fixed.

Follow the steps below to get cell data –

  1. First, locate the table.
  2. Determine the total number of rows and columns
  3. From the rows and columns count locate required cells and perform ‘getText()’ method of the WebDriver ELEMENT on the element.

Syntax -

  • WebElement baseTable = driver.findElement(By.tagName("table"));
  • List  col = driver.findElements(By.xpath(“XPath"]/table/thead/tr/th"));System.out.println("No of cols are : " +col.size());
  • List  rows = driver.findElements(By.xpath("Xpath”]/table/tbody/tr/td[1]"));System.out.println("No of rows are : " + rows.size());
  • To find 3rd rowWebElement tableRow = baseTable.findElement(By.xpath("XPath"]/table/tbody/tr[3]")); STRING rowtext = tableRow.getText();
  • To get 3rd row's 2nd column dataWebElement cellIneed = tableRow.findElement(By.xpath("XPath"]/table/tbody/tr[3]/td[2]")); String valueIneed = cellIneed.getText(); System.out.println("Cell value is : " + valueIneed);


Discussion

No Comment Found