【问题标题】:How to get all elements into list or string with Selenium using C#?如何使用 C# 使用 Selenium 将所有元素放入列表或字符串中?
【发布时间】:2014-05-11 01:00:30
【问题描述】:

我尝试过使用 WebElement(和 IWebElement)和字符串列表,但我不断收到错误消息。如何按类名获取所有元素文本的列表或字符串?我有所有 Selenium 参考资料。我需要一些 java.util dll 吗?

我应该实现一个 foreach 循环吗?

IList<IWebElement> all = new IList<IWebElement>();
all = driver.FindElements(By.ClassName("comments")).Text;

错误:无法创建抽象类或接口“System.Collections.Generic.IList”的实例

错误:“System.Collections.ObjectModel.ReadOnlyCollection”不包含“Text”的定义,并且没有扩展方法“Text”接受类型的第一个参数

【问题讨论】:

    标签: c# list selenium selenium-webdriver


    【解决方案1】:

    你可以像这样获取所有的元素文本:

    IList<IWebElement> all = driver.FindElements(By.ClassName("comments"));
    
    String[] allText = new String[all.Count];
    int i = 0;
    foreach (IWebElement element in all)
    {
        allText[i++] = element.Text;
    }
    

    【讨论】:

      【解决方案2】:

      虽然您已接受答案,但可以使用 LINQ 对其进行压缩:

      List<string> elementTexts = driver.FindElements(By.ClassName("comments")).Select(iw => iw.Text);
      

      【讨论】:

      • +1,LINQ 比公认的答案更清晰、更具可读性和可维护性。
      • @arran 我最近试过这个,发现我需要像这样构造它:List&lt;string&gt; elementTexts = new List&lt;string&gt;(driver.FindElements(By.ClassName("comments")).Select(iw =&gt; iw.Text)); 由于转换问题。
      • 我手头没有 IDE,但是很可能,因为返回的是 ReadOnlyCollection。您可以将其指定为IEnumerable,(因为ReadOnlyCollection 实现IEnumerable)所以IEnumerable&lt;string&gt; texts = driver.FindElements....ToList() 最后....List&lt;string&gt; texts = driver.FindElements(.....).ToList() 或执行您的方法。好地方。 @理查德
      【解决方案3】:
      1. 你不能创建一个而不是IList&lt;T&gt;,你必须创建一个实现接口的类的实例,例如List&lt;T&gt;:

        IList<IWebElement> all = new List<IWebElement>();
        
      2. 但是,您需要每个IWebElement 中的.Text,因此您的列表可能应该是List&lt;string&gt;

        IList<string> all = new List<string>();
        
      3. 使用foreach 将项目添加到您的列表中:

        foreach(var element in driver.FindElements(By.ClassName("comments"));
        {
            all.Add(element.Text);
        }
        

      【讨论】:

        【解决方案4】:

        你也可以这样做,它会解析菜单列表并获取列表文本。

        IWebElement menuOptions;
        menuOptions = _browserInstance.Driver.FindElement(By.XPath(".//*[@id='xxxxxxxxxxxxxxxxxx']/ul[2]/li[3]"));
        IList<IWebElement> list = menuOptions.FindElements(By.TagName("li"));
        
        //// get the column headers
        char[] character = "\r\n".ToCharArray();
        string[] Split = menuOptions.Text.Split(character);
        
        for (int i = 0; i < Split.Length; i++)
        {
            if (Split[i] != "")
            {
                _log.LogEntry("INFO", "column Text", true,
                        Split + " Text matches the expected:" + Split[i]);
            }
        }
        

        【讨论】:

          【解决方案5】:

          我用过这个表格

              public List<string> GetValidations()
              {
                  IList<IWebElement> elementList = _webDriver.FindElements(By.Id("validationList")); // note the FindElements, plural.
                  List<string> validations = new List<string>();
                  foreach (IWebElement element in elementList)
                  {
                      validations.Add(element.ToString());
                  }
                  return validations;
              }
          

          【讨论】:

            猜你喜欢
            • 2017-11-21
            • 2021-11-28
            • 1970-01-01
            • 2019-08-26
            • 1970-01-01
            • 1970-01-01
            • 2023-03-28
            • 2021-11-01
            • 1970-01-01
            相关资源
            最近更新 更多