Is there any way to handle "stale element reference: element is not attached to the page document"

Is there any way to handle “stale element reference: element is not attached to the page document” in Katalon.

Hi,

You can create a custom method which will check for stale element reference

WebDriver driver = DriverFactory.getWebDriver();

int count = 0;

while(count  < 2){
    try {
        return driver.findElement(By.xpath("<locator>"))
    } catch (StaleElementReferenceException e) {
        //Ignore
    }
    count ++;
}
1 Like

Thanks Rahul. Will try this.

Hi Lijumon_SL,
Is it worked for?.
If worked can you please share the code.

Thanks,
Swathi

Hi Swathi, It worked.For mostly it finds element second time. We created a custom method with code structured as same as above.

Wrong approach to be honest: stale references appear when you’ve navigated somewhere else or clicked somewhere and the DOM changed. You need to re-fetch WebElements after that happens…