首页 > 解决方案 > 当给定类的元素不存在于小型 DOM 中时,检查它们的不可见性需要很长时间

问题描述

在我点击任何链接之前,我确保页面中的任何地方都没有可见的加载器微调器。出于某种原因,在根本没有微调器的页面上最多需要 40 秒,而在所有其他页面上,它的闪烁速度很快。

为了确保没有微调器,我编写了这个方法:

    public static void WaitForSpinnersToDisappear(this IWebDriver driver)
    {
        var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));
        wait.Until(InvisibilityOfAllElementsLocatedBy(
            By.ClassName("spinnertype1"),
            By.ClassName("spinnertype2"),
            By.ClassName("spinnertype3")));
    }

它利用了这个功能:

    public static Func<IWebDriver, bool> InvisibilityOfAllElementsLocatedBy(params By[] locators)
    {
        return driver =>
        {

            return locators.All(locator =>
            {
                var elements = driver.FindElements(locator); //this is incredibly slow
                if (elements.Count == 0)
                {
                    return true;
                }

                return driver.FindElements(locator).All(element => !element.Displayed);
            });
        };
    }

由于某种原因 var elements = driver.FindElements(locator);,在函数中调用的InvisibilityOfAllElementsLocatedBy速度非常慢,仅对于具有短 DOM 且根本没有微调器的服务器生成页面。

标签: c#.netperformanceseleniumselenium-webdriver

解决方案


推荐阅读