Reputation: 113
I am using selenium 2 and a chrome driver and cannot seem to get the explicit wait to work no matter what I do. I am trying to click on an element which generates some data dynamically via ajax (no reloads) and then search for an element when it becomes present on the page.
This is my code
leagueNameItem.Click();
IList<IWebElement> outerTables_forEachLeague = new List<IWebElement>();
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
outerTables_forEachLeague = wait.Until<IList<IWebElement>>((d) =>
{
return d.FindElements(By.ClassName("boxVerde"));
});
The element is not found (and it is on the page for sure). The wait function does not actually 'wait' for the 10 seconds as specified ut just returns nothing. Any ideas pls?
Upvotes: 1
Views: 3202
Reputation: 27496
The problem is that FindElements
returns immediately, and returns a valid empty list object if the elements aren't found. You have two choices. You can use a single FindElement
in your wait, which throws an exception if the element doesn't exist. The WebDriverWait
object will catch that exception and retry until the element can be found.
However, since you want to return a list from your wait, you'll need to be a little more clever, which leads to your second option. Change your wait to look something like this:
leagueNameItem.Click();
IList<IWebElement> outerTables_forEachLeague = new List<IWebElement>();
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
outerTables_forEachLeague = wait.Until<IList<IWebElement>>((d) =>
{
var elements = d.FindElements(By.ClassName("boxVerde"));
if (elements.Count == 0)
{
return null;
}
return elements;
});
Upvotes: 10