如何等待 WebElement 出现在 DOM 中?

How to wait for a WebElement to be present within the DOM?

目前我正在寻找一种解决方案来等待网站中出现特定的 WebElement DOM。

目前我已经设置了以下使用 By 定位器的方法,但是我想改用 WebElement,有什么想法吗?

根据 JavaDocs:

public static ExpectedCondition visibilityOf(WebElement element):期望检查已知出现在页面 DOM 上的元素是否可见。可见性是指元素不仅被显示而且高度和宽度都大于0。

上面的现有方法检查元素是否可见并且也存在于 DOM 中,但不仅存在于 DOM.

当前使用 By 而不是 WebElement 的方法: 我可以从阅读 selenium 文档中看到,您可以等待元素的存在在 DOM; 中可见。

一个例子:

public static void waitForElementToAppearInDOM(By by, int timer) {
    try {
        WebDriver driver = getDriver();
        WebDriverWait exists = new WebDriverWait(driver, timer);
        exists.until(ExpectedConditions.presenceOfAllElementsLocatedBy(by));
    } catch(Exception e) {

    }
}

也许你可以这样试试:

WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id<locator>));

在您的代码中,您应该将 'ID' 或 'xpath' 作为定位符。 查看 ExpectedConditions 上的文档 第 5 行看起来像:

exists.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By, locator));

另外,看看这个

几个问题:

  1. Presence表示它存在于DOM但不一定是可见的、可点击的等,如果你希望元素可见,那就等待visible。
  2. 您正在使用 *Elements*,它是复数形式,它将等待定位器找到的 ALL 元素,而不仅仅是您专门寻找的元素。如果您没有唯一的定位器,这可能会导致令人困惑的故障等。如果你只想要单数,我会避免使用复数。
  3. 你应该仔细阅读the docs。存在一个 ExpectedConditions 方法可以完全满足您的需求。

public static ExpectedCondition visibilityOf(WebElement element)

您的代码应该更像

public static void waitForElementToAppearInDOM(WebElement element, int timer) {
    try {
        new WebDriverWait(getDriver(), timer).until(ExpectedConditions.visibilityOf(element));
    } catch(Exception e) {
        // don't leave an empty catch, do something with it or don't catch it.
    }
}

此方法"visibilityOfElementLocated"使用者:

示例: //等待元素可见

WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@id='text3']")));

如何使用 WebElement 作为参数检查是否存在。

    public boolean isElementPresent(WebElement element) {
        if (element != null) {
            try {
//this line checks the visibility but it's not returned. 
//It's to perform any operation on the WebElement
                element.isDisplayed(); 
                return true;
            } catch (NoSuchElementException e) {
                return false;
            } 
        } else return false;
    }

public 之前你是非常正确的, 可见。可见性意味着该元素不仅显示而且高度和宽度都大于0。上面的这个现有方法检查元素是否可见并且也存在于DOM但不仅存在于DOM

为简单起见,我将此 ExpectedCondition 改写为 检查页面 DOM 中已知存在的元素是否可见的期望

Of-coarse 这两个期望 presenceOfElementLocated()visibilityOf() 之间有很多差异。


回到您的主要问题,等待特定 WebElement 出现 使用 WebElement 作为参数是 不可能。原因很简单,WebElement在DOM中还没有被识别,只有在presenceOfElementLocatedfindElement()的期望之后才会被识别成功。

JavaDocs 中的 ExpectedCondition 列表突然支持这个概念 存在元素 如下:


总而言之,您不能将 WebElement 作为参数作为期望作为 presenceOfElementLocated() 作为 WebElement 传递尚未确定。但是一旦元素通过 findElement()presenceOfElementLocated() 被识别,这个 WebElement 就可以传递一个参数。

总结:

  • presenceOfElementLocated(By locator):此期望用于检查页面的 DOM 中是否存在元素 present。这并不一定意味着该元素是 visible.

  • visibilityOf(WebElement element):此期望用于检查已知存在于页面 DOM 上的元素是否可见。可见性意味着该元素不仅 displayed 而且具有 heightwidth大于0.