Automation Task: Extracting Product Names and Ratings with Selenium in C#

In this blog, we’ll explore a practical Selenium task often encountered in interview scenarios for QA engineers. The task involves navigating a website, interacting with menu elements, and extracting product information, specifically product names and their ratings. We’ll break down the task step-by-step and provide the complete C# code to accomplish it.

Task Overview

  • Navigate to Next’s website.
  • Mouse over the “Men” section in the menu.
  • Click on the “Jeans” category.
  • Extract and display the list of product names with their respective ratings.

Setup the Environment

Ensure you have the following installed:

  • Visual Studio
  • Selenium WebDriver NuGet package
  • ChromeDriver executable

Below is the step-by-step explanation of the code:

a) Initialize WebDriver and Navigate to the URL
The task begins by launching the browser, navigating to the target URL, and maximizing the browser window for better visibility. We also set an implicit wait to handle loading times.

IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://www.next.co.uk/");
driver.Manage().Window.Maximize();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);

b) Handle Cookies Popup
Many websites display cookie consent popups. We handle this using XPath to locate and interact with the “Accept” button.

driver.FindElement(By.XPath("//*[@id='onetrust-accept-btn-handler']")).Click();

c) Mouse Over to “Men” and Navigate to “Jeans”

Using Selenium’s Actions class, we simulate a mouse hover action to reveal the submenu and then click the “Jeans” category.

IWebElement MenMenuLink = driver.FindElement(By.XPath("//*[@id='meganav-link-2']/div"));
Actions action = new Actions(driver);
action.MoveToElement(MenMenuLink).Perform();
driver.FindElement(By.XPath("//span[contains(text(),'Jeans')]")).Click();

d) Extract Product Ratings and Names
The product ratings and names are located in a specific attribute of elements. We use a loop to iterate through these elements, extract their title attribute, and process the data.

IList<IWebElement> ratingListEle = driver.FindElements(By.XPath("//span[@data-testid='product_summary_star-rating']"));
foreach (IWebElement ratingEle in ratingListEle)
{
    string ratingText = ratingEle.GetAttribute("title").ToString();

    // Split rating and product name
    var list = ratingText.Split(' ');
    double finalRatingInDouble = Convert.ToDouble(list[list.Length - 2]);

    var ProNameList = ratingText.Split('|');
    string ProductName = ProNameList[0];
    Console.WriteLine(ProductName + " : " + finalRatingInDouble);
}

Here is the complete code for the task:

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Interactions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpAutomation.CSharpBasic
{
    internal class AutomationTaskDemo
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello there");
            IWebDriver driver = new ChromeDriver();
            driver.Navigate().GoToUrl("https://www.next.co.uk/");
            driver.Manage().Window.Maximize();
            driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);

            //Handle Cookies Popups
            driver.FindElement(By.XPath("//*[@id='onetrust-accept-btn-handler']")).Click();

            // Mouse Over
          IWebElement MenMenu =  driver.FindElement(By.XPath("//a[@title='MEN']"));
            Actions actions = new Actions(driver);
            actions.MoveToElement(MenMenu).Perform();
            driver.FindElement(By.XPath("//span[contains(text(),'Jeans')]")).Click();

            //find the list of reating test

            IList<IWebElement> ratingListEle = driver.FindElements(By.XPath("//span[@data-testid='product_summary_star-rating']"));

            foreach (IWebElement ratingElement in ratingListEle)
            {
                string ratingText = ratingElement.GetAttribute("title").ToString();
                // Split String

                var list = ratingText.Split(' ');

                double rating = Convert.ToDouble(list[list.Count()-2]);

                var productName = ratingText.Split('|');

                String finalProjectName = productName[0];

                Console.WriteLine(finalProjectName+ " : "+ rating);

            }

            

        }
    }
}

Output