How to handle webtable in Selenium Webdriver

asked 3 months ago

A web table (also known as an HTML table) is a structure used on web pages to display data in rows and columns, similar to a spreadsheet or an Excel file.

It is created using the table tag in HTML, and typically contains:

     thead – defines the header section (optional)
     tbody – defines the body containing the actual data rows
     tr – defines a row
     th – defines a header cell (usually the first row)
     td – defines a regular data cell

There are different operations we can perform in the web table using a Selenium web driver.

The most frequent operation is:

- Verify the expected records in the web table

- Click the element/button in the web table when there are matching records

Navigate to https://www.qafeast.com/demo, and click the Web table tab.

Press F12 on the keyboard and inspect web table

 

 

Verify the expected records in the web table

To verify the records in the webtable , we need to know how many rows and columns are there. The rows will be dynamic and columns are static. To iterate the webtable the number of rows will be taken "//tr" gives the number of rows and "th" denotes the column.

// To find the webtable
WebElement table =  driver.findElement(By.xpath("//table[@id='webtable-table']/tbody"));
// Get all the rows
List rowsList = table.findElements(By.tagName("tr")); // Get row items
List columnsList = null;
rowsList.remove(0); // to remove the first row which contains the column names in th
// Iterate each row
for (WebElement row : rowsList) {
    System.out.println(columnsList.get(0).getText()); // To get the first column of the row
    System.out.println(columnsList.get(1).getText()); // To get the second column of the row
    System.out.println(columnsList.get(2).getText()); // To get the third column of the row
    System.out.println(columnsList.get(3).getText()); // To get the fourth column of the row
    System.out.println(columnsList.get(4).getText()); // To get the fifth column of the row
     # write a code logic to verify the expected text
}
 
 

Click the element/button in the web table when there is a matching record

WebElement table = driver.findElement(By.xpath("//table[@id='webtable-table']/tbody"));
List rowsList = table.findElements(By.tagName("tr")); // Get row items
List columnsList = null;
rowsList.remove(0); // to remove the first row which contains the column names in th
for (WebElement row : rowsList) {
  System.out.println(columnsList.get(0).getText()); // To get the first column of the row
// Example
// To click if the text matches the expected Result
if(columnsList.get(1).getText().equals("Tiger Nixon")){
    columnsList.get(1).click();
    // break
}
}

 

Using Selenium  Csharp

 

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace ConsoleApp1
{
    internal class SeleniumTest
    {
        static IWebDriver driver;
        static void Main(string[] args)
        {
           
            driver = new ChromeDriver(@"\\Driver\\chromedriver.exe");
            //Implicit Wait:
            driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
            driver.Navigate().GoToUrl("https://qafeast.com/demo");

            ((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].scrollIntoView(true);", driver.FindElement(By.XPath("//label[text()='Webtable']")));
            driver.FindElement(By.XPath("//label[text()='Webtable']")).Click();
            //Verify the expected records in the web table
            IList rowsList = driver.FindElements(By.TagName("tr")); // Get row items
            IList colmsList;
            // Iterate each row
            Console.WriteLine("dfrgergertheth "+rowsList.Count);
            for (int row= 1; row < rowsList.Count;row++)
            {
                colmsList = driver.FindElements(By.XPath("//tr["+row+"]//td"));
                Console.WriteLine("dfrgergertheth " + colmsList.Count);
                // Iterate each column
                foreach (var col in colmsList)
                {
                    Console.WriteLine(col.Text);
                }
            }
            //Click the element/button in the web table when there is a matching records
            IList rowsList1 = driver.FindElements(By.TagName("tr")); // Get row items
            IList colmsList1;
            // Iterate each row
            for (int row = 1; row < rowsList.Count; row++)
            {
                colmsList1 = driver.FindElements(By.XPath("//tr[" + row + "]//td"));
                // Iterate each column
                foreach (var col in colmsList1)
                {
                    if (col.Text.Equals("New York"))
                    {
                        col.Click();
                        Console.WriteLine("matching record is clicked");
                    }
                }
            }
            driver.Quit();  
        }
    }
}