简体   繁体   English

Selenium C#:有没有办法在继续之前等待加载覆盖层隐藏?

[英]Selenium C#: Is there a way to wait for loading overlay to hide before proceeding?

So I've been creating different explicit waits after doing tasks like updating contact info, submitting forms, etc because every time I do this, a loading overlay will pop up for a few seconds.因此,在执行更新联系信息、提交表单等任务后,我一直在创建不同的显式等待,因为每次执行此操作时,加载叠加层都会弹出几秒钟。 I just realized it's the same exact loading code on each page.我刚刚意识到每个页面上的加载代码完全相同。 Instead of waiting for results to show on the page or something else to appear, I just want to wait for this loading overlay to hide.而不是等待结果显示在页面上或其他东西出现,我只想等待这个加载覆盖隐藏。 Is it possible to write some reusable code to wait for this overlay to hide before proceeding?是否可以编写一些可重用的代码来等待此叠加层隐藏,然后再继续?

The overlay code:叠加代码:

<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>

When the loading display is present, the div attributes will become:当加载显示出现时,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>

If your version of .net supports ExpectedConditions , you can straight away use the below solution如果您的 .net 版本支持ExpectedConditions ,您可以立即使用以下解决方案

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

If it does not support then the below solution may work.如果它不支持,则以下解决方案可能有效。

//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;
    }
}

and use it like this :并像这样使用它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM