【问题标题】:Selenium C#: Is there a way to wait for loading overlay to hide before proceeding?Selenium C#:有没有办法在继续之前等待加载覆盖隐藏?
【发布时间】:2021-12-09 12:18:18
【问题描述】:

所以我在执行更新联系信息、提交表单等任务后一直在创建不同的显式等待,因为每次我这样做时,加载覆盖都会弹出几秒钟。我刚刚意识到每个页面上的加载代码完全相同。而不是等待结果显示在页面上或出现其他内容,我只想等待这个加载覆盖隐藏。是否可以编写一些可重用的代码来等待此叠加层隐藏后再继续?

叠加代码:

<div id="progress" style="display: none;" role="status" aria-hidden="true">
    <div id="divOverlay"></div>
    <div id="divLoading">
        <img id="loading" src="/loading.gif" />
    </div>
</div>

当加载显示出现时,div属性会变成:

<div id="progress" style="display: block;" role="status" aria-hidden="false">
  <div id="divOverlay"></div>
  <div id="divLoading">
    <img id="loading" src="/loading.gif" />
  </div>
</div>

【问题讨论】:

    标签: c# .net selenium selenium-webdriver selenium-chromedriver


    【解决方案1】:

    如果您的 .net 版本支持ExpectedConditions,您可以直接使用以下解决方案

    var wait = new WebDriverWait(Driver.Instance, TimeSpan.FromSeconds(10));
    wait.until(ExpectedConditions.invisibilityOfElementLocated(By.XPath("//div[@id='progress']")));
    

    如果不支持,则以下解决方案可能有效。

    //returns as soon as element is not visible, or throws WebDriverTimeoutException
    protected void WaitUntilElementNotVisible(By searchElementBy, int timeoutInSeconds)
    {
        new WebDriverWait(_driver, TimeSpan.FromSeconds(timeoutInSeconds))
                        .Until(drv => !IsElementVisible(searchElementBy));
    }
    
    private bool IsElementVisible(By searchElementBy)
    {
        try
        {
            return _driver.FindElement(searchElementBy).Displayed;
    
        }
        catch (NoSuchElementException)
        {
            return false;
        }
    }
    

    并像这样使用它:

    WaitUntilElementNotVisible(By.Id("progress"), 10);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-28
      • 1970-01-01
      • 1970-01-01
      • 2021-05-19
      • 2012-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多