【问题标题】:Selenium waiting for loader disappearsSelenium 等待加载程序消失
【发布时间】:2020-10-27 08:59:12
【问题描述】:

我正在尝试添加方法,该方法将使用显式等待来等待元素不可见:

    public static void WaitForElementNotVisible(this IWebDriver driver, By by, int timeoutInSeconds = 5)
    {
        try
        {
            WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
            wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.InvisibilityOfElementLocated(by));
        }
        catch (WebDriverTimeoutException)
        {
            throw new WebDriverTimeoutException("\n" +
                "**************************************\n" +
                "Below element should be not visible:\n" +
                "**************************************\n" +
                by.ToString() + " \n" +
                "Timeout after: " + timeoutInSeconds.ToString() + " seconds\n" +
                "______________________________________________________");
        }
    }

测试页面使用反应,在页面操作之间(在页面之间切换)有一个加载器图标,持续时间少于几秒到 3/4 秒,具体取决于操作。

该加载器的 DOM 如下所示:

<span>
   <div class="Loader__background" style="display: block; position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; background-color: unset; z-index: 10;">
      <div class="Loader__foreground" style="display: table; width: 100%; height: 100%; text-align: center; z-index: 20; color: white;">
         <div class="Loader__message" style="display: table-cell; vertical-align: middle;">
            <div class="sc-fzqBkg jCOtDD">
               <div class="sc-fzqPZZ kyyPPI"></div>
            </div>
         </div>
      </div>
   </div>
</span>

当加载器消失时,只有一个空的跨度部分:

无论我使用什么定位器:

public static void WaitForInnerLoaderDisappear(this IWebDriver driver, int timeoutInSeconds = 30)
{
    driver.WaitForElementNotVisible(By.XPath("//div[@class = 'Loader__background']"), timeoutInSeconds);
}

等待没有按应有的方式工作。实际上它会等待并点击页面元素,但它的速度非常慢。加载程序不再“眼睛”可见”,而是在两次点击之间等待 10 到甚至 40 秒,而不是向前移动。似乎某种状态仍然可见。问题可能出在哪里?

编辑

正如@Dazed 建议的那样,我已经进行了更改,现在我正在尝试检查集合大小。

  public static void WaitForElemNotDisplayed_byXPath(this IWebDriver driver, int timeoutInSeconds = 30)
    {
        try
        {
            WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
            IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
            int loaderAmount = int.Parse(js.ExecuteScript($"return document.querySelectorAll(\"div[class*='Loader_background']\").length;").ToString());
            wait.Until(webDriver => loaderAmount == 0);
        }
        catch (Exception) { throw; }
    }

将wait.Untill与Javascript执行器混合是否正确?它运行得更快,但仍然出现片状,所以我认为有些地方不正确。

【问题讨论】:

标签: c# selenium selenium-webdriver dom


【解决方案1】:

我要做的一件事是在元素加载和消失后对其进行截图。这有助于我识别不同之处。很多时候只是风格的改变。

您可以尝试以下方法并传入 xpath。

  WaitForElemNotDisplayed_byXPath("//div[@class='myEle']");

       public static void WaitForElemNotDisplayed_byXPath(string elementClass)
    {
        try
        {
            _wait.Until(webDriver => driver.FindElements(By.XPath(elementClass)).Count == 0);
        }
        catch (Exception) { throw; }
    }

你也可以给它添加一个计时器:

        public static void WaitForElementNoLongerDisplayed_byXPathTime(string value)
    {
        try
        {
            var wait = new DriverWait(driver, new TimeSpan(0, 0, 40));
            wait.Until(webDriver => Driver.FindElements(By.XPath(value)).Count == 0);
        }
        catch (Exception) { throw; }
    }

【讨论】:

  • 感谢您的回答,实际上我是按照您发布的方式制作的,但结果是它有效,但速度极慢。我做了一些改变,而不是唱 FindElements 我试过用 JavaScript Executor 应该更快,但我不确定代码是否好。你能看看我编辑的问题吗?
猜你喜欢
  • 2014-11-23
  • 1970-01-01
  • 2018-07-31
  • 1970-01-01
  • 2020-11-21
  • 2014-07-19
  • 2016-07-28
  • 1970-01-01
  • 2018-03-20
相关资源
最近更新 更多