I’m new to Selenide and I’m experiencing an issue with $$x method. Here’s a simple method that should return a collection of elements (store items actually) by an xpath, the defaut implicit wait here is 10 seconds:
public ElementsCollection getSearchResults() {
if ($$x(searchResultsXpath).isEmpty()) {
Assertions.fail("Web page doesn't contain search results");
}
return $$x(searchResultsXpath);
}
The issue is that it may return less elements than it should if it happens before all elements are loaded on the page.
Also the thing is that I may not know the actual number of elements, it can be zero or more. If I don’t use any filters on the page (but I may) it’s basically about 16 of elements but I shouldn’t rely on that number anyway.
I know that I can specifically wait for the page to fully load with executing a script, but I’m trying to use only Selenide and, if really need, Selenium.
I know that I might seem ignorant here, but I’m really stuck.
I tried using “should” methods with CollectionConditions but they don’t seem to solve the problem. I’m 60% sure there’s is some condition that may help, but I don’t see it. Is there a way to avoid using Selenium’s presenceOfAllElementsLocated()?
In a nut I want the method to return the actual number of elements that the page will contain after it’s fully loaded, whether there are zero or more elements.
There are several ways to get all the data after they are loaded completely.
- Wait for the Ajax call completely, then get the data. You may check this answer Selenium wait for Ajax content to load – universal approach
- If the page has a ‘loading’ indicator, wait for the loading element to
disappear. then get the data. - If the data are in a table, wait for the table to have data or show
empty content. then get the data. - Wait for a constant seconds. this is not recommended.