i

Selenium Step By Step Guide

Handling Web Tables

Identifying HTML Table

First, we need to identify the web tables in the HTML source code. Whenever we see a table structure in a web page, it is surrounded by a tag. If we look inside the table tag, we can see and and

tags as well.
tags represent the row and column of the table respectively. Headings in a web table is tagged by tag.

 

Now we should be able to understand the HTML tables of a web page. Let us roam around the Precise Testing Solutions official web page www.precisetestingsolution.com/functional-automation-testing.php to find an excellent example on web tables.

Now if we click on the inspect element on the web browser, we get the HTML source code of this web table as below:

Locating the web table cell and fetching the cell text

Suppose we need to find the second row and the first column, i.e., "Functional Testing” text. First, we need to check the absolute Xpath of this text. Let’s figure out the web element using the Xpath for this text from the web page.

WebElement element2=driver.findElement(By.xpath("/html/body/div[1]/div[3]/div[2]/div/div[1]/table/tbody/tr[2]/td[1]"));

Now we can simply use the getText() method to get the text out of the element which is nothing but “Functional Testing” text.

String sText=element.getText();

Dynamic rows and columns:

Let’s make it more dynamic by assigning the variables to row and columns.

String sRow=”2”;

String sCol=”1”;

This time row and column tags will be replaced by dynamic variables sRow and sCol.

WebElement element2=driver.findElement(By.xpath("/html/body/div[1]/div[3]/div[2]/div/div[1]/table/tbody/tr["+sRow+"]/td["+sCol+"]"));

Now it has become more dynamic in nature that we have made the row and column tags dynamic. Using this approach we can locate any element in web table and also text of the element can be fetched using getText() method.

String eleText=element2.getText();

System.out.println(eleText);

Console Output: Output text will be “Functional Testing” at console.

Finding a text-based cell in a web table:

In some cases, the table is large enough to look for a cell, and we want to do a dynamic search that looks for a cell element by a text. Suppose we want to find the "JMeter" cell element in the web table.

Here we need to look at each cell containing the text "JMeter," and once it matches the same, we can get the element of the cell as well.

public class App

{

    public static void main( String[] args )

    {

        System.setProperty("webdriver.chrome.driver","C:\\Users\\chromedriver.exe");

        WebDriver driver=new ChromeDriver();

        driver.get("https://www.precisetestingsolution.com/functional-automation-testing.php");

        String givenText="JMeter";

// Let us assign a string variable to collect the cell texts

        String eleText="";

//Nested for loops to check each cell until the expected one is matched.

               for(int i=2; i<=7;i++)

               {

                               for(int j=1;j<=3;j++)

                               {

                                               WebElement element2=driver.findElement(By.xpath("/html/body/div[1]/div[3]/div[2]/div/div[1]/table/tbody/tr["+i+"]/td["+j+"]"));

                                               eleText=element2.getText();

                                               if(eleText.contains(givenText))

                                               {

                                                               System.out.println("Expected Element is located and is: "+eleText);

                                               }

                                              

                               }

               }

              

        driver.quit();

    }

}