I have an UI like this:
I have to choose each attachment and click to download or delete.
To download:
// get the list of radio buttons
List<WebElement> attachments = driver().findElements(By.className("select-attachment"));
for (WebElement attachment : attachments) {
attachment.click();
driver().findElement(By.className("download-attachment")).click();
}
This works fine, the attachments will be clicked and downloaded.
But I am facing a problem with detele test case. Because when I click Delete button, there will be an Alert
dialog to ask "Are you sure blah", after I accept the dialog, I get the StaleElementReferenceException
To delete:
// get the list of radio buttons
List<WebElement> attachments = driver().findElements(By.className("select-attachment"));
for (WebElement attachment : attachments) {
attachment.click();
driver().findElement(By.className("delete-attachment")).click();
Alert alert = alert().get();
alert.accept();
}
This works fine for the first attachment, but after I accept the Alert
, the element attachement
is no longer attached to the DOM, so when it loops to the second radio button, I get the StaleElementReferenceException
.
I know with Stale element we have to findElement
again. But since I have a list in for
loop, I don't know how to handle it.
How can I still use for
loop and be able to handle Stale element?