Similar questions were already asked a million times but I still haven't figured out the correct way how to solve this problem.
First question: Is it bad practice to iterate through a List of Webelements in Selenium?
I am a big fan of Lists in Java. Usually I find all the WebElements I am interested in and iterate over them. But if you do that then you often get a StaleElementException. I know what a StaleElementException is but I can't figure out the right way to prevent them while still using Lists.
What would you guys do in the following scenario: I have a bunch of tiles in my webapp that I need to click and check whether the result is ok. I would love to do something like this but it will often result in a StaleElementException:
List<WebElement> tiles = driver.findElements(By.className("box"));
for (WebElement tile: tiles) {
//at this point I already get a stale element exception quite often.
//How can I use Lists without getting a stale element at all?
tile.click()
}
Is there a way to still use lists and never get a stale element? Can I re-find a element without using a "By" but by using an "old" WebElement?
Something like:
WebElement newWebElement = driver.findElement(oldWebElement)
Otherwise how would I even describe in my Code to find that one old WebElement from those dozen tiles I have since they don't have unique IDs?