简体   繁体   中英

How to iterate through IWebElement list using Selenium C#

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

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); }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM