【问题标题】:Wait for multiple elements to become invisible Selenium Java等待多个元素变得不可见 Selenium Java
【发布时间】:2017-08-07 05:38:36
【问题描述】:

我正在使用此代码来检查隐身性:

WebDriverWait wait = new WebDriverWait(driver,40);
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath(<some xpath>)));

如果网页中只有 一个 元素对应于网页中的 xpath,则此方法非常有效。

我正在尝试为其编写脚本的网页中有 三个,我需要 selenium 来等待所有三个。

注意:我没有使用绝对 xpath。

【问题讨论】:

  • 你有 3 个具有相同 xpath 的 webelement 吗?
  • 是的,问题不是很清楚吗?

标签: java selenium selenium-webdriver


【解决方案1】:

ExpectedConditions.invisibilityOfElementLocated 检查第一个元素。在您的情况下,您可以编写自己的 ExpectedCondition 实现,您必须检查是否为找到的每个元素显示对象。

例如(未测试):

private static void waitTillAllVisible(WebDriverWait wait, By locator) {

    wait.until(new ExpectedCondition<Boolean>() {

        @Override
        public Boolean apply(WebDriver driver) {

            Iterator<WebElement> eleIterator = driver.findElements(locator).iterator();
            while (eleIterator.hasNext()) {
                boolean displayed = false;
                try {
                    displayed = eleIterator.next().isDisplayed();
                }
                catch (NoSuchElementException | StaleElementReferenceException e) {
                    // 'No such element' or 'Stale' means element is not available on the page
                    displayed = false;
                }
                if (displayed) {
                    // return false even if one of them is displayed.
                    return false;
                }
            }
            // this means all are not displayed/invisible
            return true;
        }
    });
}

【讨论】:

  • 看起来可能有效,但调试时遇到问题。请修复“没有类型参数”和“方法没有从超类覆盖”错误
  • 在我的机器上编译得很好。你使用的是哪个版本的 java 和 selenium?
  • Java 1.8、Selenium 3.0.1
  • 好的,我刚刚导入了 ExpectedCondition。我的坏
  • 花了我一段时间来测试它。工作得很好谢谢。虽然我需要在加载网页之前让您的代码正常工作。
猜你喜欢
  • 2019-10-21
  • 2017-08-14
  • 2016-06-09
  • 2019-11-02
  • 1970-01-01
  • 2019-07-07
  • 2014-05-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多