Selenium - Wait until element is NOT visible

后端 未结 10 2083
生来不讨喜
生来不讨喜 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 07:58
    public void WaitForElementNotVisible(string id, int seconds)
        {
    
            try
            {
                var wait = new OpenQA.Selenium.Support.UI.WebDriverWait(driver, TimeSpan.FromSeconds(seconds));                   
                wait.Until(driver1 => !visibility(id));
                Console.WriteLine("Element is not visible..");
            }
            catch (WebDriverTimeoutException)
            {
                Assert.Fail("Element is still visible..");
            }
    
    
        }
    
    
        bool visibility(string id)
        {
            bool flag;
            try
            {
                flag = driver.FindElement(By.Id(locator)).Displayed;
            }
            catch (NoSuchElementException)
            {
                flag = false;
            }
            return flag;
        }
    
    0 讨论(0)
  • 2021-02-02 08:00

    Yes, you can create your own ExpectedCondition, just revert visible to not visible.

    Here is how to do it in python:

    from selenium.webdriver.support.expected_conditions import _element_if_visible
    
    class invisibility_of(object):
    
        def __init__(self, element):
            self.element = element
    
        def __call__(self, ignored):
            return not _element_if_visible(self.element)
    

    and how to use it:

    wait = WebDriverWait(browser, 10)
    wait.until(invisibility_of(elem))
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2021-02-02 08:12

    The following should wait until the element is no longer displayed i.e. not visible (or time out after 10 seconds)

    var wait = new WebDriverWait(Driver.Instance, TimeSpan.FromSeconds(10));
    wait.Until(driver => !driver.FindElement(By.Id("processing")).Displayed);
    

    It will throw an exception if an element cannot be found with the id processing.

    0 讨论(0)
  • 2021-02-02 08:13

    You can use driver.FindElements for access to non-existing items.

    wait.Until(d => d.FindElements(By.Id("processing")).Count == 0);
    
    0 讨论(0)
  • 2021-02-02 08:15

    Yes, it's possible with method invisibilityOfElementLocated

    wait.until(ExpectedConditions.invisibilityOfElementLocated(locator));
    
    0 讨论(0)
提交回复
热议问题