我有一个使用 WebDriver 的 Java Selenium 测试套件。 我有一个通用的等待函数,它接受一个 CSS 选择器并等待元素出现并可见,然后再使用 WebDriverWait 进行断言。不过,现在,我想要一些东西等待 DOM 中某种类型的所有元素不再可见,然后再继续。确切的例子是我有许多图像正在加载,我想等待它们的加载微调器消失后再继续。还没有找到可以为我解决这个问题的东西,所以把它贴在这里。 有什么线索吗?
谢谢!
public void waitForPageToLoad(WebDriver webDriver, String cssToWaitFor) {
WebDriverWait wait = new WebDriverWait(webDriver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(cssToWaitFor)));
}
最佳答案
检查 invisibilityOfElementLocated
预期的条件。 Reference .来自文档:
invisibilityOfElementLocated public static ExpectedCondition<java.lang.Boolean> invisibilityOfElementLocated(By locator) An expectation for checking that an element is either invisible or not present on the DOM. Parameters: locator - used to find the element
所以你可以像这样使用它:
public void waitForInvisibility(WebDriver webDriver, String cssToWaitFor) {
WebDriverWait wait = new WebDriverWait(webDriver, 20);
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector(cssToWaitFor)));
}
或者您可以查看 public static ExpectedCondition<java.lang.Boolean> not(ExpectedCondition<?> condition)
,由文档执行:
An expectation with the logical opposite condition of the given condition.
关于java - 带有 WebDriver 的 Selenium - 等待,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22143465/