0 votes
1 view
in Devops and Agile by (19.8k points)

In Selenium 2.0, I have no idea how to traverse through an HTML table on a webpage. In selenium2.0 Javadoc, I found two classes "TableFinder" and "TableCellFinder", but I couldn't find any examples.

I want to do something like this:

RowCount=Get how many rows are there in the HTML table

for each row of the table

{

   column_count=Get column count

   for each column

   {

      cell_value=get_text_from(row,col);

      Do something with cell_value

   }

}

How can I get the text from each of the table cells?

1 Answer

0 votes
by (63.4k points)

Here's a solution using selenium 2.0 classes.

import java.util.List;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.ie.InternetExplorerDriver;

public class WebTableExample 

{

    public static void main(String[] args) 

    {

        WebDriver driver = new InternetExplorerDriver();

        driver.get("http://localhost/test/test.html");      

        WebElement table_element = driver.findElement(By.id("testTable"));

        List<WebElement> tr_collection=table_element.findElements(By.xpath("id('testTable')/tbody/tr"));

        System.out.println("NUMBER OF ROWS IN THIS TABLE = "+tr_collection.size());

        int row_num,col_num;

        row_num=1;

        for(WebElement trElement : tr_collection)

        {

            List<WebElement> td_collection=trElement.findElements(By.xpath("td"));

            System.out.println("NUMBER OF COLUMNS="+td_collection.size());

            col_num=1;

            for(WebElement tdElement : td_collection)

            {

                System.out.println("row # "+row_num+", col # "+col_num+ "text="+tdElement.getText());

                col_num++;

            }

            row_num++;

        } 

    }

}

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...