wait.Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.ClassName(className))不返回任何元素

问题描述:

我需要使用 WebDriverWait 查找 IReadOnlyCollection< IWebElement> ,以确保元素已在页面上呈现.

I need to find IReadOnlyCollection<IWebElement> using WebDriverWait to make sure that elements had been rendered on page.

这是我的代码

 WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeout));
 return wait.Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.ClassName("TextInput")));

此代码超时失败. 这意味着在页面上找不到具有给定类名的任何元素. 我在原始代码之前中添加了这一行代码,以确保存在元素

This code failing on timeout. Meaning that could not find any elements on page with given class name. I added this line of code BEFORE my original code just to make sure that elements are present

 var allInputs1 = container.FindElements(By.ClassName("textInput"));

该行将按预期返回元素.

And that line returns elements as expected.

所以我的结论是 wait.Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.ClassName("TextInput"))) 无法正常运行,因为无法找到页面上肯定存在的元素.

So my conclustion is that wait.Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.ClassName("TextInput"))) doesn't work as expected since that couldn't find elements that are for sure present on page.

使用 WebDriverWait 查找元素数组的最佳方法是什么?

What is the best way to find array of elements using WebDriverWait?

您的结论是错误的.使用FindElements,您只需确保存在元素.

Your conclusion is wrong. With FindElements you just make sure that elements are present.

VisibilityOfAllElementsLocatedBy的API文档状态:

期望检查网页上是否存在所有元素 与定位器匹配的是可见的.可见性意味着元素 不仅显示,而且高度和宽度为 大于0.

An expectation for checking that all elements present on the web page that match the locator are visible. Visibility means that the elements are not only displayed but also have a height and width that is greater than 0.

显然,存在是不可见的.

我认为您应该尝试ExpectedConditions.PresenceOfAllElementsLocatedBy