【问题标题】:Selenium - loop through elements check if collection has a valueSelenium - 遍历元素检查集合是否有值
【发布时间】:2021-04-08 09:33:39
【问题描述】:

我将具有特定值的 DOM 元素作为文本。 如果下面是“Uno”

<span class="custom-field-value" xpath="1">Uno</span>

元素具有相同的类,但仅在提到的文本值上有所不同。

我想检查特定 Webelement 的文本是否在至少一个 span 元素中可见

已经完成的步骤:

  • 定义 IWebElements

  • 我将用于比较的元素文本值的创建列表:

      IList<string> customFields = new List<string> { customField1, customField2, customField3, customField4 };
    
  • 比较方法有问题。我需要将列表传递给方法,从每个列表中获取 Webelement .Text 值,然后遍历所有元素并检查每个文本是否等于和一个列表位置。我假设最好的方法是将 for 循环与 linqu 混合。

      public bool ValidateCustomFields(IList<string> customFields)
      {
          for (int i = 0; i < CartCustomFields.Count; i++)
          {
              CartCustomFields.ElementAt(i).Text.
              CartCustomFields.Any(element => element.FindElement(By.XPath(".")).Text)
          }
      }
    

【问题讨论】:

  • 您创建的 customFields 数据与网页的顺序是否相同?
  • 我认为这应该适合你CartCustomFields.Any(element =&gt; element.FindElement(By.XPath(".")).Text==customFields[i])

标签: c# selenium selenium-webdriver selenium-chromedriver


【解决方案1】:

如果您正在检查特定文本是否显示在 UI 上并且它可以在多个元素中,为什么不使用 xpath 搜索所有元素

//span[@class = 'custom-field-value' and text() = 'YOUR_TEXT'] 

然后检查列表是否为空?

【讨论】:

  • 但是如何传递 IWebElements 中的每个列表元素然后进行比较呢?
  • 如果您的目标只是找到带有该文本的 webElements,那么您不必进行比较...或者我错过了什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-16
  • 2021-12-22
  • 2016-12-09
  • 2014-05-13
  • 2021-05-05
  • 2010-11-23
  • 1970-01-01
相关资源
最近更新 更多