简体   繁体   English

如何使用 Selenium C# 遍历 IWebElement 列表

[英]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我尝试迭代一个 IWebElement 列表并打印每个 h2,但问题只是它打印第一个 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我不知道发生了什么,因为我打印过元素文本并且打印正常,但是当我尝试获取所有 h2 时,它只打印第一个 h2

To print the product names from the <h2> tags you can use the following Locator Strategy :要从<h2>标签打印产品名称,您可以使用以下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 : 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); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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