wait.Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.ClassName(className)) 没有 return 任何元素

wait.Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.ClassName(className)) doesn't return any element

我需要使用 WebDriverWait 找到 IReadOnlyCollection 以确保元素已呈现在页面上。

这是我的代码

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

此代码因超时而失败。 这意味着无法在具有给定 class 名称的页面上找到任何元素。 我在 BEFORE 我的原始代码之前添加了这行代码只是为了确保元素存在

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

那行 returns 元素符合预期。

所以我的结论是 wait.Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.ClassName("TextInput"))) 没有按预期工作,因为找不到页面上肯定存在的元素。

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

你的结论是错误的。使用 FindElements,您只需确保存在元素即可。

VisibilityOfAllElementsLocatedBy 的 API 文档指出:

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