Working with tables in Selenium in node

In this topic, You will learn how to perform below operations on table using Selenium.
  • Find total number of rows and columns in a table
  • Read a value from the table cell
  • Click on elements inside table cells
  • Finding total number of rows and columns in a table
  • //table//tr – This XPATH can be used to get all tr elements inside the table. Note that if there are nested tables, it will get all nested tr elements as well. To get only immediate child tr elements, you will have to use children property which returns collection of child tr elements. Alternatively you can use “//table/tr” XPATH to find only immediate child tr elements. Note that we have used single / which means find only immediate children.
  • //table//th -This XPATH can be used to find total number of columns in a table. Note that all column headings should be marked with th tag. Also note that if one th tag spans multiple columns, it will be counted only once.
  • //table//tr[1]//td – This XPATH finds all td elements (table cells) in the first row of the table. Note that all nested td elements will be found using this XPATH expression.
Here is a simple example on Tables.

var tableCells = driver.findElements(By.tagName(“//table//td”));

tableCells .then(function (elements) {

console.log(“Total number of cells -> ” + elements.length+ “

//Print data from each cell
for (var i=0; i < elements.length; i++){
console.log(txt + “

Web development and Automation testing

solutions delivered!!