I tried all ways that I know in Selenium web driver, but I could not resolve the Stale Element Reference Exception. I have tried implicit wait, explicit wait, and fluent wait in Selenium web driver.
My HTML looks like the screenshot below. I have to click all li options in this list.

If I loop this using foreach, for, or Iterator I can click the first option but the second option throws a Stale Element Reference Exception.
My selenium code is:
List<WebElement> chapterNames = driver.findElements(By.xpath("//*[@id='ctl00_PageContent_ddlreviewCat_DropDown']/child::div/ul/li"));
//List<WebElement> chapterQues = driver.findElements(By.xpath("//*[@id='ctl00_PageContent_grdReviewDocDetailList_ctl00']/child::tbody/tr/td[1]"));
WebElement ChapterNamedrpdwn = driver.findElement(By.id("ctl00_PageContent_ddlreviewCat_Input"));
Thread.sleep(2000);
for (int i = 0; i < chapterNames.size(); i++) {
try {
wait=new WebDriverWait(driver,30);
ChapterNamedrpdwn.click();
wait.until(ExpectedConditions.refreshed(ExpectedConditions.visibilityOfAllElements(chapterNames)));
chapterNames.get(i).click();
System.out.println(chapterNames.get(i).getAttribute("innerHTML")+ " clicked..");
Thread.sleep(3000);
}
catch(Exception e) {
System.out.println(e.getMessage());
}
}
Error Message is:
stale element reference: element is not attached to the page document (Session info: chrome=86.0.4240.198)
For documentation on this error, please visit:
https://www.seleniumhq.org/exceptions/stale_element_reference.html