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
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;
}
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))
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);
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
.
You can use driver.FindElements
for access to non-existing items.
wait.Until(d => d.FindElements(By.Id("processing")).Count == 0);
Yes, it's possible with method invisibilityOfElementLocated
wait.until(ExpectedConditions.invisibilityOfElementLocated(locator));