user2917239
user2917239

Reputation: 898

Selenium - Wait until element is NOT visible

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

var wait = new WebDriverWait(Driver.Instance, TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.ElementIsVisible(By.Id("processing")));

Is it possible to tell driver to wait until that element is NOT visible?

Upvotes: 44

Views: 104036

Answers (10)

Jason D
Jason D

Reputation: 2087

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

Upvotes: 5

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

wait.Until(d => d.FindElements(By.Id("processing")).Count == 0);

Upvotes: 2

Sergey
Sergey

Reputation: 341

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

Upvotes: 1

Me Like Cookies
Me Like Cookies

Reputation: 103

var wait = new WebDriverWait(Driver.Instance, TimeSpan.FromSeconds(10));
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("processing")));

The idea is to wait until element is not visible. First line sets wait time that element has to disappear; here it's 10 seconds. Second line uses selenium to check if condition "invisibilityofElementLocated" is met. Element is found by its id as in topic case, that is id="processing". If element doesn't disappear in the requested period of time, a Timeout exception will be raised and the test will fail.

Upvotes: 7

Anoop
Anoop

Reputation: 11

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

Upvotes: 1

p3nGu1nZz
p3nGu1nZz

Reputation: 1715

Use invisibility method, and here is an example usage.

final public static boolean waitForElToBeRemove(WebDriver driver, final By by) {
    try {
        driver.manage().timeouts()
                .implicitlyWait(0, TimeUnit.SECONDS);

        WebDriverWait wait = new WebDriverWait(UITestBase.driver,
                DEFAULT_TIMEOUT);

        boolean present = wait
                .ignoring(StaleElementReferenceException.class)
                .ignoring(NoSuchElementException.class)
                .until(ExpectedConditions.invisibilityOfElementLocated(by));

        return present;
    } catch (Exception e) {
        return false;
    } finally {
        driver.manage().timeouts()
                .implicitlyWait(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
    }
}

Upvotes: 2

bornfromanegg
bornfromanegg

Reputation: 2918

I know this is old, but since I was searching for a solution to this, I thought I'd add my thoughts.

The answer given above should work if you set the IgnoreExceptionTypes property:

var wait = new WebDriverWait(Driver.Instance, TimeSpan.FromSeconds(10));
wait.IgnoreExceptionTypes = new[] { typeof(NoSuchElementException) }
wait Until(driver => !driver.FindElement(By.Id("processing")).Displayed);

Upvotes: 0

user3916823
user3916823

Reputation: 1

In the code below which is used to stop the driver for couple of seconds

System.Threading.Thread.Sleep(20000);

Upvotes: -17

Alpha
Alpha

Reputation: 14046

Yes, it's possible with method invisibilityOfElementLocated

wait.until(ExpectedConditions.invisibilityOfElementLocated(locator));

Upvotes: 49

Russ Cam
Russ Cam

Reputation: 125488

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.

Upvotes: 13

Related Questions