I think you just dialed a bit too far with
time = driver.findElements(By.xpath(TIME_CHECK)).iterator().next().getAttribute("class");
with . approach.
Usually what we do, if you do want to use this convoluted one - liner ( in c# for example ) is call ( this is a pseudocode )
.iterator().firstOrDefault()
and that way we get the value.
In Java I think there are methods such as tail(x) / head(x) / first(x) invoked on the collection ( here it is probably the return of findElement()) which will return an array from which you can take [0] element ( which may fail to null exception )
The Java pattern is to first check for null and than perform value access.
So, you can see that while one - liner can look nice, it would require a bit higher effort at understanding internals.
Simpler pattern is to scroll using iterator
while(driver.findElements(By.xpath(TIME_CHECK)).iterator().next()) {
x = driver.findElements(By.xpath(TIME_CHECK)).iterator().current() / item()
myValue = x.method / property
break or something else
}
This takes a few more lines, but is easy to follow and debug. Personally, I use both approaches, depending on context, but when in doubt the easier one.
---
I was under impression this is where error occurs ( please correct me if I am wrong ).
If my understanding is correct, the rest looks like somewhat working approach.
Except it's probably an pattern specific to Selenium.
In an application, if I wanted to be able to perform some action on items, found in the cycle of the iterator above, while cycle is still running, I'd use a
yeld
pattern, and organize concurrency by means of function invoked in yeld block with item / value passed as parameter. Give me a moment to find an example.
Darn it, yeld is c# pattern, ported to java only as of release 13.
So, just save the value to list in the cycle and run over it once iterator is done.
It shouldn't take too long.