stale element reference: element is not attached to the page document
java, list, selenium-webdriver
Solution
What is the line which gives exception ??
The reason for this is because the element to which you have referred is removed from the DOM structure
I was facing the same problem while working with IEDriver. The reason was because javascript loaded the element one more time after i have referred so my date reference pointed to a nonexistent object even if it was right there in the UI. I used the following workaround:
try {
WebElement date = driver.findElement(By.linkText(Utility.getSheetData(path, 7, 1, 2)));
date.click();
}
catch(org.openqa.selenium.StaleElementReferenceException ex)
{
WebElement date = driver.findElement(By.linkText(Utility.getSheetData(path, 7, 1, 2)));
date.click();
}
See if the same can help you !
Problem
I have list which has multiple links under each section. Each section has same links I need to click a particular link under each section. I have written the below code but when it executes it gives me `stale element reference: element is not attached to the page document` error. This is my code: ``` public static void main(String[] args) throws InterruptedException { WebDriver driver = new ChromeDriver(); driver.navigate().to("url......"); driver.findElement(By.id("Login1_txtEmailID")).sendKeys("hourbank5@....com"); driver.findElement(By.id("Login1_txtPassword")).sendKeys("Testing1*"); driver.findElement(By.id("Login1_btnLogin")).click(); List<WebElement> LeftNavLinks=driver.findElements(By.xpath("//*[@id='sliding-navigation']//a")); Thread.sleep(1000); String ben="Benefit Status"; String[] linkTexts = new String[LeftNavLinks.size()]; int i = 0; for (WebElement e : LeftNavLinks) { linkTexts[i] = e.getText(); System.out.print(i+" " + linkTexts[i]+"\n"); if(linkTexts[i].equals(ben)) { String BenefitStatLi="//*[@id='sliding-navigation']/li[%s]/a"; System.out.print(i+" " + linkTexts[i]+"\n"); driver.findElement(By.xpath(String.format(BenefitStatLi,i))).click(); driver.findElement(By.xpath("//* [@id='divContentHolder']/div[1]/a[1]")).click(); } i++; } } } ``` This is the HTML structure is as below ``` <div id="ucAdminMenu_divMenu"> <ul id="sliding-navigation"> <li class="sliding-element"> <a href=" ">Claims Status</a> </li> <li class="sliding-element"> <a href=" ">Eligibility Status</a> </li> <li class="sliding-element"> <h3>Section-1</h3> </li> <li class="sliding-element"> <a href=" ">Forms and Documents</a> </li> <li class="sliding-element"> <a href=" HourBank.aspx?id=002">Hour Bank</a> </li> <li class="sliding-element"> <h3>Section-2</h3> </li> <li class="sliding-element"> <a href=" ">Benefit Status</a> </li> <li class="sliding-element"> <a href=" ">Forms and Documents</a> </li> <li class="sliding-element"> <h3>Section-3</h3> </li> <li class="sliding-element"> <a href=" ">Forms and Documents</a> </li> <li class="sliding-element"> <h3>Testing Fund</h3> </li> <li class="sliding-element"> <a href=" ">Benefit Status</a> </li> <li class="sliding-element"> <a href=" ">Order ID Card</a> </li> </ul> </div> ``` The Error Trace is: ``` Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document ```