【问题标题】:Count Method on IEnumerable<IWebElement> does not return correct countIEnumerable<IWebElement> 上的计数方法不返回正确的计数
【发布时间】:2015-12-20 03:45:19
【问题描述】:

我遇到了一个问题,在集合中我可以看到 2 个元素,但该集合上的 count 方法仅返回 1。

解决方法是在 count 方法之前添加 100 毫秒的等待,它可以解决问题,但我仍然不确定它为什么会发生。

我正在使用以下代码来获取计数:

IEnumerable<IWebElement> myEmptyElements = null;
var actualElementCount = 0;
myEmptyElements = allElements.Where(e => e.Text != "");

var expectedElementCount = table.RowCount;

//Adding this wait until we can find some better solution
//Thread.Sleep(100);
actualElementCount = myEmptyElements.Count();//this count returns less then number of items in the collection

【问题讨论】:

  • 你什么时候执行这段代码。您应该等待 selenium 加载并运行页面。
  • @RichardSchneider,元素已正确加载,因此我可以在集合中看到它们。它的 count 方法返回集合中错误的项目数。allElements 是从一个单独的函数传递的,该函数首先检查是否所有元素都正确加载然后填充它。

标签: c# selenium-webdriver ienumerable


【解决方案1】:

主要原因(很可能是)该元素需要更长的时间才能存在于 dom 中。我会使用PendElements使用适当的选择器并应用预期的条件并使用PresenceOfAllElementsLocatedBy等待IWebelements的集合。请参考文档here

【讨论】:

  • allElements 在等待 elemetns 出现后被填充,因此集合具有预期的项目。我不明白的问题是为什么 allElements.count 返回的 count WaitFor(this By by) { var wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(TimeoutInSeconds));尝试 { wait.Until(ExpectedConditions.ElementIsVisible(by));catch(catch expections) } }
  • 那么,如果你把 100ms 静态等待它返回 2?
猜你喜欢
  • 1970-01-01
  • 2017-07-07
  • 2021-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-17
  • 1970-01-01
相关资源
最近更新 更多