1

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%3A7072561011%2Cp_36%3A-30000%2Cp_72%3A2491149011&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

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
pupy frias
  • 109
  • 3
  • 9
  • This sounds like an [X-Y problem](http://xyproblem.info/). Instead of asking for help with your solution to the problem, edit your question and ask about the actual problem. What are you trying to do? – undetected Selenium Nov 23 '21 at 03:43
  • @DebanjanB i try to scrape amazon web and i want to get items title, price and link – pupy frias Nov 23 '21 at 04:01
  • Does this answer your question? [selenium webdriver (c#) - Best way to iterate through a select menu - Then verify an elements style updates?](https://stackoverflow.com/questions/26614990/selenium-webdriver-c-best-way-to-iterate-through-a-select-menu-then-verif) – cruisepandey Nov 23 '21 at 07:31

1 Answers1

1

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%3A7072561011%2Cp_36%3A-30000%2Cp_72%3A2491149011&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%3A7072561011%2Cp_36%3A-30000%2Cp_72%3A2491149011&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);
    }
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • it works but i want to get the item title, price and link. Is there a method or library like Beautifulsoup in Python to better handle html? – pupy frias Nov 23 '21 at 04:56
  • [Selenium](https://stackoverflow.com/questions/54459701/what-is-selenium-and-what-is-webdriver/54482491#54482491) itself would be enough to achieve that. You may like to raise a new ticket for your new requirement. – undetected Selenium Nov 23 '21 at 04:58