StaleElementReferenceException is not going away even after reinitializing the elements list | Selenium Forum
P
Pravin V. Kumbhare Posted on 15/10/2020

I'm using the function findElements and collecting the elements returned by the function in a List of WebElements as below:

List<WebElement> headerLinks = driver.findElements(By.xpath("//ul[@class='navbar-nav']/li/a"));

Using a for loop, I'm performing some operations over each of the element as below:

for (WebElement e:headerLinks) {
System.out.println(e.getText());
String url = e.getAttribute("href");
System.out.println(url);
softAssert.assertTrue(validateResponseCode(url), "Response code not 200");
e.click();
System.out.println(driver.getTitle());
driver.get("https://www.miraeassetmf.co.in/");
System.out.println("----------");
headerLinks = driver.findElements(By.xpath("//ul[@class='navbar-nav']/li/a"));
}

As can be seen in above snippet, I'm also re-initializing the List of WebElements to avoid the StaleElementReferenceException.

Still I get the same StaleElementReferenceException in the output as below:

List size: 6
----------
About Us
(current)
https://www.miraeassetmf.co.in/about-us
Response code: 200
Know About Mirae Asset India & Global Investments | Mirae Asset
----------
@AfterMethod
FAILED: cnnHeaderLinksTest
org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
(Session info: chrome=86.0.4240.75)

 


A
Ashish Thakur Replied on 16/10/2020

@AfterMethod

 

Where is this printed from in logs?


P
Pravin V. Kumbhare Replied on 21/10/2020

It's printed from the AfterMethod annotation (in the TestBase class).

Here is the AfterMethod function:

@AfterMethod
public void finalizeExtentRep() throws InterruptedException {
System.out.println("@AfterMethod");
extent.flush();
Thread.sleep(10000);
driver.quit();
}


A
Ashish Thakur Replied on 22/10/2020

Please send me your java file.

Sorry for late reply


Related Posts