Selenium与WebDriver - 等

时间:2014-03-03 09:53:14

标签: 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)));
}

1 个答案:

答案 0 :(得分:3)

检查invisibilityOfElementLocated预期的情况。 Reference。来自doc:

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),该文档由我们执行:

  

对给定的逻辑相反条件的期望   条件。

相关问题