0

I have an UI like this:

enter image description here

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?

Ragnarsson
  • 1,715
  • 7
  • 41
  • 74
  • what screen do you see after accepting the alert? – Adrian Jimenez Apr 20 '20 at 16:19
  • @AdrianJimenez: The Alert is just overlayed on top of the attachments page. So after the alert, the focus is back to attachments page, but the element is no longer attachment to the DOM – Ragnarsson Apr 21 '20 at 07:28
  • after alert.accept, past this line List attachments = driver().findElements(By.className("select-attachment")); if element does not exisit, then you need to execute steps to make attachments visible again – Adrian Jimenez Apr 21 '20 at 16:12

0 Answers0