我有这样的用户界面:
我必须选择每个附件并单击以下载或删除。
去下载:
// 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();
}
这工作正常,附件将被单击并下载。
但我正面临 detele 测试用例的问题。因为当我点击删除按钮时,会有一个Alert
对话框询问“你确定吗”,在我接受对话框后,我得到了StaleElementReferenceException
删除:
// 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();
}
这适用于第一个附件,但在我接受 之后Alert
,该元素attachement
不再附加到 DOM,所以当它循环到第二个单选按钮时,我得到StaleElementReferenceException
.
我知道我们必须findElement
再次使用 Stale 元素。但由于我有一个for
循环列表,我不知道如何处理它。
我怎样才能仍然使用for
循环并能够处理陈旧的元素?