首页 > 解决方案 > ExpectedConditions.presenceOfAllElementsLocatedBy 有多少?

问题描述

ExpectedConditions.presenceOfAllElementsLocatedBy(locator)

webDriver如何知道定位器定位了多少元素?

标签: seleniumselenium-webdriver

解决方案


如果您查看代码,它只会抓取与提供的定位器匹配的所有元素。它唯一捕捉到的是StaleElementReferenceException. 完整的代码在下面,带有指向源的链接。

/// <summary>
/// An expectation for checking that all elements present on the web page that
/// match the locator.
/// </summary>
/// <param name="locator">The locator used to find the element.</param>
/// <returns>The list of <see cref="IWebElement"/> once it is located.</returns>
public static Func<IWebDriver, ReadOnlyCollection<IWebElement>> PresenceOfAllElementsLocatedBy(By locator)
{
    return (driver) =>
    {
        try
        {
            var elements = driver.FindElements(locator);
            return elements.Any() ? elements : null;
        }
        catch (StaleElementReferenceException)
        {
            return null;
        }
    };
}

https://github.com/DotNetSeleniumTools/DotNetSeleniumExtras/blob/master/src/WaitHelpers/ExpectedConditions.cs


推荐阅读