Selenium - Wait until element is NOT visible

后端 未结 10 2081
生来不讨喜
生来不讨喜 2021-02-02 07:29

In the code below, I attempt to wait until an element is visible:

var wait = new WebDriverWait(Driver.Instance, TimeSpan.FromSeconds(10));
wait.Until(ExpectedCon         


        
10条回答
  •  清酒与你
    2021-02-02 08:11

    As of 2020, ExpectedConditions is deprecated in .NET.

    For some reason, I was not able to make IgnoreExceptionTypes work.

    The only solution that worked for me was the one proposed by Anoop. One thing I like about his solution is it returns as soon as the element becomes invisible.

    I generalized his solution a bit as follows:

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

    Usage:

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

提交回复
热议问题