java - 带有 WebDriver 的 Selenium - 等待

标签 java selenium selenium-webdriver webdriver

我有一个使用 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/

相关文章:

java - 将值四舍五入到两位小数但仍然在 Eclipse (Java) 中返回 10 位小数?

java - 关闭所有浏览器窗口的方法,无论警报如何 Selenium

selenium - 如何从警告框中获取文本

javascript - JavaScript 可以与 Selenium 2 对话吗?

javascript - 隐藏元素 Selenium IDE

selenium - 测试立即失败,出现未知错误 : DevToolsActivePort file doesn't exist when running Selenium grid through systemd

java - android Map错误膨胀类 fragment

java - Web 服务将参数传递到另一个类/项目

java - 无法访问tomcat webapp中的文件

java - Appium/Selenium - 断言声明为字段的元素不显示