Selenium C# – WebTable Data Extraction

Selenium C# – WebTable Data Extraction
Introduction
Web tables are a popular way to show structured data in web apps. With Selenium, you can easily explore these tables, grab the data you need, and even flip through several pages. In this guide, I'll show you how to automate web table data extraction and print all the entries across multiple pages using C# Selenium.
Code Implementation
Below is a C# Selenium script that opens a website, navigates to a table, extracts all the rows and columns from the table, and prints the data for each entry.
Example: Extract and Print Web Table Data in Selenium C
csharpCopyEditusing Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.DevTools.V85.Profiler;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium.Support.UI;
using SeleniumExtras.WaitHelpers;
using System.Globalization;
namespace SACSSAuto.QA.SeleniumPractice
{
[TestClass]
public class Selenium_83
{
/****
* Sample Code Definition :- Selenium - WebTable - Print All Table Data
* Developed By :- Pankaj Kumar
* Date of Creation :- 31-Jan-2025
* Project :- SACSSAUTO - C# [Web / Mobile / API]
*/
[TestMethod]
public void Selenium_Test()
{
Console.WriteLine("Selenium - WebTable - Print All Table Data");
// Initialize Chrome WebDriver
IWebDriver driver = new ChromeDriver();
// Maximize the browser window
driver.Manage().Window.Maximize();
// Set implicit wait time
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
// Open the URL
driver.Navigate().GoToUrl("https://practice.solveautomation.in");
// Wait for 5 seconds
Thread.Sleep(5000);
// Click on WebTable link
driver.FindElement(By.XPath("//a[normalize-space()='WebTable']")).Click();
// Wait for 5 seconds
Thread.Sleep(5000);
// List to store CountryInfo objects
List<CountryInfo> countries = new List<CountryInfo>();
// Loop to navigate through all pages
int i = 1;
while (i < 19)
{
// Find the table body
IWebElement table = driver.FindElement(By.XPath("//table//tbody"));
// Get all rows of the table
IList<IWebElement> rows = table.FindElements(By.TagName("tr"));
// Loop through all rows and extract data
foreach (IWebElement row in rows)
{
IList<IWebElement> columns = row.FindElements(By.TagName("td"));
if (columns.Count >= 4) // Ensure there are enough columns
{
string country = columns[1].Text;
string capital = columns[2].Text;
string currency = columns[3].Text;
string language = columns[4].Text;
// Store data in the list
countries.Add(new CountryInfo(country, capital, currency, language));
}
}
// Click on the "Next" button
driver.FindElement(By.XPath("//button[normalize-space()='>']")).Click();
i++;
}
// Print all data
Console.WriteLine("Total Country Found in List ==> " + countries.Count);
foreach (var country in countries)
{
Console.WriteLine(country.ToString());
}
// Quit the driver
driver.Quit();
}
// CountryInfo class to store table data
public class CountryInfo
{
public string Country { get; set; }
public string Capital { get; set; }
public string Currency { get; set; }
public string Language { get; set; }
// Constructor
public CountryInfo(string country, string capital, string currency, string language)
{
Country = country;
Capital = capital;
Currency = currency;
Language = language;
}
// Override ToString for easy printing
public override string ToString()
{
return $"Country: {Country}, Capital: {Capital}, Currency: {Currency}, Language: {Language}";
}
}
}
}
Steps Explained
1. Navigating to WebTable
Accesses the SolveAutomation practice website and navigates to the WebTable section.
2. Extracting Table Data
Loops through table rows and columns to collect Country, Capital, Currency, and Language data.
3. Pagination Support
Handles pagination by clicking the “Next” button until all 18 pages are traversed.
4. Data Storage and Print
Stores each entry in a CountryInfo object and prints the total count and details.
Expected Output
Collects all countries and associated data across multiple table pages.
Prints each country’s info in the format:
Country: India, Capital: New Delhi, Currency: INR, Language: Hindi
Conclusion
Automating web table data extraction with Selenium is a fantastic way to handle testing or data scraping. This example shows you how to loop through paginated tables, grab the data, and store it neatly using C#.
Subscribe to my newsletter
Read articles from Pankaj Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
