waiting for an element not to be present

130 views
Skip to first unread message

bjm3...@gmail.com

unread,
Aug 7, 2021, 2:48:06 AM8/7/21
to Selenium Users
Hi
The wait helpers are very useful functions. But it seems they can wait only for an element to exist (Until...)
Is there a wait to say "wait while condition is still fullfilled" ?
I have implemented this in a loop but I was wondering if Selenium Wait could be used to the same purpose.
Example, click some element and wait for some other element to disappear
Thanks in advance

Adrian

unread,
Aug 8, 2021, 6:02:07 PM8/8/21
to Selenium Users
Hi,
To wait for a element not to be present use, invisibilityOfElementLocated:
new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector('#loading-content')))

Cheers,
Adrian.

joseph...@gmail.com

unread,
Aug 9, 2021, 2:14:45 PM8/9/21
to Selenium Users
Another possible thing to try (at least with Java, I'm not sure if it's in the other languages) is the ExpectedConditions.not method, which you can wrap around another ExpectedConditions.

An example would be something like:
new WebDriverWait(driver, 20).until(ExpectedConditions.not(ExpectedConditions.presenceOfElementLocated(By.cssSelector('#loading-spinner'))));

You can see the list of existing ExpectedConditions methods for Java here:
https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html

bjm3...@gmail.com

unread,
Aug 9, 2021, 2:30:05 PM8/9/21
to Selenium Users
the not function seems what I was looking for. However it seems that it has not been implemented in the DotNet binding of SeleniumExtras.WaitHelpers.ExpectedConditions

Chris Bloom

unread,
Aug 9, 2021, 3:02:29 PM8/9/21
to Selenium Users
You don't need to use built in functions, just write your own. It'll run continually until what you put inside Until() returns as true (or an object) or times out per the WebDriverWait you setup. Quick sample from my code waiting to ensure a driver is open with available windows before doing anything.

wait.Until(d => d!= null && d.WindowHandles != null && d.WindowHandles.Count > 0);

or waiting for javascript ajax calls to complete
wait.Until(d => (bool)(d as IJavaScriptExecutor).ExecuteScript("return jQuery.active == 0"));


This is how the ElementIsVisible wait helper is implemented

Lookup these 2 very simple functions in there, you should be able to do something similar Where they returned the object when it's displayed you can just return false if it's displayed and true if it's not.

public static Func<IWebDriver, IWebElement> ElementIsVisible(By locator)
&
private static IWebElement ElementIfVisible(IWebElement element) 

joseph...@gmail.com

unread,
Aug 9, 2021, 3:06:25 PM8/9/21
to Selenium Users
C# has some abilities that Java did not natively possess for a long time, so many things can be done just by using the C# language to write your own functions that behave similarly to what is in the ExpectedConditions class.

Here is the Java source code for the not method, which might help you come up with an idea for implementing your own version in C#.
https://github.com/SeleniumHQ/selenium/blob/trunk/java/src/org/openqa/selenium/support/ui/ExpectedConditions.java#L836

Here is some explanation from Jim Evans, the maintainer of the DotNet bindings, on why it doesn't make sense to move everything Java has to DotNet just because Java has it.
http://jimevansmusic.blogspot.com/2018/03/deprecating-parts-of-seleniums-net.html

bjm3...@gmail.com

unread,
Aug 10, 2021, 12:06:18 AM8/10/21
to Selenium Users
Thanks a lot for these answers. Quite useful.
In fact, in the first place, I wrote my wait functions. 
I thought the waithelpers would be a better choice. 
In the end, a mix of both is perfectly fine

Reply all
Reply to author
Forward
0 new messages