Home > Blockchain >  How to iterate through IWebElement list using Selenium C#
How to iterate through IWebElement list using Selenium C#

Time:11-23

I try to iterate an IWebElement list and print every h2, but problem is just its prints the first h2

this my code

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.Collections.Generic;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Selenium();
        }

        private static void Selenium()
        {
            ChromeOptions options = new ChromeOptions();
            options.AddArguments("start-maximized","disable-infobars", "--no-sandbox", "--disable-dev-shm-usage",
                "--disable-gpu", "--disable-extensions", "--allow-running-insecure-content", "--ignore-certificate-errors",
                $"--user-data-dir={AppDomain.CurrentDomain.BaseDirectory}/User Data");

            using (IWebDriver driver = new ChromeDriver(options)) {

                driver.Navigate().GoToUrl("https://www.amazon.com/-/es/s?i=mobile&bbn=7072561011&rh=n:7072561011,p_36:-30000,p_72:2491149011&dc&fs=true&language=es&qid=1614961572&rnid=2491147011&ref=sr_pg_");
                IList<IWebElement> elements = driver.FindElements(By.ClassName("sg-col-inner"));

                for (int i = 1; i < elements.Count; i  )
                {
                    Console.WriteLine(elements[i].FindElement(By.XPath("//span[@class=\"a-size-base-plus a-color-base a-text-normal\"]")).Text);

                }
                Console.ReadLine();
            };
            
        }
    }
}

This is the result

Apple iPhone 8 GSM desbloqueado, 64GB - espacio gris (restaurado)
Apple iPhone 8 GSM desbloqueado, 64GB - espacio gris (restaurado)
Apple iPhone 8 GSM desbloqueado, 64GB - espacio gris (restaurado)
Apple iPhone 8 GSM desbloqueado, 64GB - espacio gris (restaurado)
Apple iPhone 8 GSM desbloqueado, 64GB - espacio gris (restaurado)
Apple iPhone 8 GSM desbloqueado, 64GB - espacio gris (restaurado)
Apple iPhone 8 GSM desbloqueado, 64GB - espacio gris (restaurado)
Apple iPhone 8 GSM desbloqueado, 64GB - espacio gris (restaurado)
Apple iPhone 8 GSM desbloqueado, 64GB - espacio gris (restaurado)
Apple iPhone 8 GSM desbloqueado, 64GB - espacio gris (restaurado)
Apple iPhone 8 GSM desbloqueado, 64GB - espacio gris (restaurado)
Apple iPhone 8 GSM desbloqueado, 64GB - espacio gris (restaurado)
Apple iPhone 8 GSM desbloqueado, 64GB - espacio gris (restaurado)
Apple iPhone 8 GSM desbloqueado, 64GB - espacio gris (restaurado)

I don't know what's happening, because i print ever element text and its prints ok, but when i try with to get all h2 just its prints the first h2

CodePudding user response:

To print the product names from the <h2> tags you can use the following Locator Strategy:

  • CssSelector:

    driver.Navigate().GoToUrl("https://www.amazon.com/-/es/s?i=mobile&bbn=7072561011&rh=n:7072561011,p_36:-30000,p_72:2491149011&dc&fs=true&language=es&qid=1614961572&rnid=2491147011&ref=sr_pg_");
    IList<IWebElement> elements = driver.FindElements(By.CssSelector("h2>a>span"));
    foreach (IWebElement element in elements)
    {
        Console.WriteLine(element.Text);
    }
    
  • XPath:

    driver.Navigate().GoToUrl("https://www.amazon.com/-/es/s?i=mobile&bbn=7072561011&rh=n:7072561011,p_36:-30000,p_72:2491149011&dc&fs=true&language=es&qid=1614961572&rnid=2491147011&ref=sr_pg_");
    IList<IWebElement> elements = driver.FindElements(By.XPath("//h2/a/span"));
    foreach (IWebElement element in elements)
    {
        Console.WriteLine(element.Text);
    }
    
  • Related