0

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

2
  • StaleElementReferenceException is occurred when the element is manipulated and it's reference has been destroyed and still you are trying to interact with the element whose reference is not available in the DOM, So can you check you're xpath. Commented Nov 30, 2020 at 17:55
  • Thanks.. Is there any other option to resolve this issue? rather than getting every element for every refresh. Because there are almost 20 options in the list so how to get the elements for each refresh and loop them again. kindly let me know if you have any idea about this issue. Commented Dec 1, 2020 at 6:28

1 Answer 1

1

In your code you wait for a refresh - if your DOM is changed, old references are not usable anymore (your driver tells you this). You will have to reacquire reference to LIs in the next every time after you clicked it again

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks.. but if I used to run the code without refresh in wait still it returns the same error. I thought that if I choose an option in the list the page had refreshed and DOM elements re-creating again, so old elements are not used for looping. Is there any other option to resolve this issue? rather than getting every element for every refresh.Because there are almost 20 options in the list so how to get the elements for each refresh and loop them again.
This exception means that something has changed in web page, and reference you ahave obtained throgh web driver is no longer usable. You have to reacquire them again after any refresh.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.