Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have had no success with getting a simple line of code to implement the selenium implicit wait. Can you send me to a reference? The C# version is unworkable. Here is the C# code.
using (IWebDriver driver = new FirefoxDriver())
{
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
driver.Url = "http://somedomain/url_that_delays_loading";
IWebElement myDynamicElement = driver.FindElement(By.Id("someDynamicElement"));
}

What I have tried:

Extensive search of the internet. Only can find code for C# and Java.
Posted
Updated 7-Feb-18 5:55am
v2
Comments
David_Wimbley 6-Feb-18 22:57pm    
Can you post the C# version, i've done a bunch of work with selenium and have found a wait function that works for me but I don't know what you mean by "implicit wait"
BobbyStrain 7-Feb-18 11:00am    
David I added the code to the question. There doesn't seem to be much use of Selenium with vb.net.
David_Wimbley 7-Feb-18 11:20am    
So it looks to me you are trying to wait until something on the page loads correct? Or rather, you want selenium to wait until it can see whatever is on the page before it begins...ex: You want selenium to wait until it can see/interact with "someDaynamicElement" right?

1 solution

I think you may want to look into WebDriverWait class in selenium. I mostly work with selenium in C# so hopefully this code is correct in VB.net (i didn't test it).

First i'll start with the C# example

C#
var wait = new WebDriverWait(driver, new TimeSpan(0, 0, 45));
wait.IgnoreExceptionTypes(typeof(StaleElementReferenceException));
			
wait.Until(webDriver =>
{
	return webDriver.FindElements(By.Id("someDynamicElement")).Where(m => m.Displayed).Any();
});


So the idea is that wait.Until will continue to loop until a condition is met that is true (the xml doc on this method is stupid, it says "until a condition is neither null nor false"...so until its true).

Also, you give it a max timeout of 45 seconds so if it finds what it needs in 2 seconds, great, if it takes 10 seconds then thats fine too...you don't have to require selenium to wait X number of seconds unnecessarily is the idea.

So converting it to vb.net i think would be something like this.

VB.NET
Dim wait As New WebDriverWait(webDriver, TimeSpan.FromSeconds(seconds))
wait.Until(Function(webDriver)
  Return webDriver.FindElement(By.Id("someDynamicElement")).Enabled And webDriver.FindElement(By.Id("someDynamicElement")).Displayed
End Function)


The only downside to this is that on every page load you'll need to use the above sample for at least the first element so that way you don't have to resort to a Thread.Sleep or something like that.
 
Share this answer
 
Comments
BobbyStrain 8-Feb-18 11:17am    
David, Thank you for your help. I'll work on it. But this code appears to target a single element by ID, not all elements. I am looking for code that will wait for any element, not specific. But, I can work with your code. It's something I can understand, unlike much of the code I have found.
David_Wimbley 8-Feb-18 11:24am    
Yea unfortunately I'm not aware of a way that will wait for all elements that works...this was what i found worked for me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900