Hi I am new to Selenium
I am using the Java library
, already tried both the Chrome
and the Firefox
drivers.
I am running a loop. The interesting thing is that the loop works sometimes 3, 2 times, it does not always fail in the same iteration. I assume it has to do with some sort of race condition (like waiting the page to load). If I run in debug mode it seems to work perfectly.
I already tried suggestions from other answers like to wait explicitly
and implicitly
but still not helping. Maybe if you see the code you can give me a hand.
This goes inside a loop.
WebDriverWait wait = new WebDriverWait(driver,20);
WebElement searchResults = driver.findElement(new By.ById("searchresults"));
searchResults = searchResults.findElement(new By.ByClassName("table"));
wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.tagName("a")));
List<WebElement> list=searchResults.findElements(By.tagName("a"));
for(WebElement w: list) {
result.add(w.getAttribute("href")); //EXCEPTION HAPPENS ALWAYS HERE
}
SOLUTION
The solution is a total hack. I still do not understand, but it does the job. If someone understands why please let me know.
I just move all the waits up and it performs better. I also took the suggestion of @Cyril to re-run the iteration if the exception was thrown along some data checks to make sure I got all I wanted.
WebDriverWait wait = new WebDriverWait(driver,20);
wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(new By.ById("searchresults")));
wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(new By.ByClassName("table")));
wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.tagName("a")));