Stale element reference: element is not attached to the page document.

Stale Element Exception happens when the element is not attached to DOM anymore, by that mean, when you have already used the object in previous steps then refreshing or reloading the page and try to use that object again it will throw Stale Element Exception. In Selenium, you can redeclare the variable, but in Katalon since it uses Object Repo, you can’t really do that. So what I found to solve the problem is that, I used different xpath for the object or just try to run your script couple times it will pass.

Actually just do a findTestObject again and it will work…

What do you mean by that? you are going to have to do find test object anyways in order to do any commands like Click. but if the same Object is used more than one time it throws Stale Element. Could you please give me more detail on what you mean by doing findtestobject again?

In your Test Case, you should be able to do something like:‘Object Repository/myTestObject’))
// do more stuff
WebUI.navigateToUrl(‘somewhere else’)
// do stuff‘Object Repository/myTestObject’))

If you would save a reference of the TestObject (or WebElement) like:

TestObject myTestObject = findTestObject(‘Object Repository/myTestObject’)
// do more stuff
WebUI.navigateToUrl(‘somewhere else’)
// do stuff

Then the second click will probably throw a stale element reference. I’m not totally sure here, since I haven’t looked at the source code of TestObject… it just depends how they work

For sure this will fail:

WebElement element = driver.findElement(By.xpath(…xpath…))
WebUI.navigateToUrl(‘somewhere else’)

The 2nd one will fail since the element was found on page1, but after navigating to page 2, it doesn’t exist anymore in your browser.

1 Like

StaleElementRefrenceExceptions are thrown any time your code references an element that is no longer in the same state that it was when you originally located it. The two most common scenarios where this can happen are:

1.) The entire page has reloaded, either because a redirect happened after clicking a link, or a browser refresh, etc. This is what @r.klein1 mentioned.
2.) A subset of elements on the page has changed (usually by AJAX), and the element you are referencing exists in that subset.

Keep in mind that visually, that element may not have changed at all (it looks the same in the HTML, even after the page has reloaded). But in reality, this element was probably removed completely, then rebuilt from scratch by a page reload, by AJAX, etc.

The stack is quite long, but basically, holding a reference to a TestObject is fine, and should never itself lead to a StaleElementReferenceException. A TestObject is really just a container for a locator. Then, whenever you call the WebUI methods, properties from the TestObject are used to locate the element at runtime. This means that elements are re-located every time a WebUI method is called.

Knowing this, the only chance that a StaleElementReferenceException can really be thrown here is if, with unlucky timing, the element is located, then the page reloads before the actual action (click(), or whatever) can occur.

This can usually be mitigated 100% by using wait conditions to make sure the page is done changing before trying to locate/manipulate any elements.

1 Like

Thank you it is good to know that the elements are relocated evertime WebUI method is called. So far the only thing has been working for me was to use a different xpath or rerunning the test. I will def try out waits next time if i have same issue.

The smart wait is awesome here… however has some downsides, since it injects a javascript which listens to ajax events and sometimes screws up your page :wink: Else the WebUI.waitForPageLoad(5) also helps. It should wait until the page has loaded, but if it just takes 1/2 second, it won’t wait 5 seconds like a normal delay would do.

Hi @Russ_Thomas I’m trying to select all the options from the list I had written the code something like below, the loop is iterating at the first time and it is working fine at the second time getting an exception.

,Could you please help me.

WebDriver driver = DriverFactory.getWebDriver()
String options = driver.findElement(“branch”)).getText().split(“\n”);
Select List = new Select(driver.findElement(“branch”)));
List element = List.getOptions();
int itemSize = element.size();

for(int i = 0; i < itemSize ; i++){
String optionsValue = element.get(i).getText();
WebUI.selectOptionByLabel(findTestObject(‘Service_GS/Download’), ‘GatePass’, false)
LocalDate today =

String formattedDate = today.format(DateTimeFormatter.ofPattern('dd/MM/yyyy'))


def newdate = new SimpleDateFormat('ddmmyyyy').format(new SimpleDateFormat('dd/mm/yyyy').parse(formattedDate))


WebUI.setText(findTestObject('Service_GS/InvFromDate'), newdate)

WebUI.setText(findTestObject('Service_GS/InvToDate'), newdate)

WebUI.delay(5)'Service_GS/Download_Btn'), FailureHandling.STOP_ON_FAILURE)


Hello, everyone,

I’m having a similar issue and I tried some of the suggested fixes and my test is running fine when in normal browser, but it’s failing with “stale element reference: element is not attached to the page document” if I run it in headless mode.

I’m running the following steps:

In step 16 in the same page a modal window is opened. And then in step 23 it should find this filter - it sometimes passes, sometimes doesn’t. I’m using only XPath locators and no matter how many Waits I place, it still doesn’t find it…

Anytime an issue is inconsistent, it generally points to a wait issue. Try adding a hard wait (of maybe 5 seconds) to confirm this. Then you’ll need to implement an appropriate wait condition for the actual element in question.

1 Like

Ok, thanks I’ll keep that in mind. Today the test has passed, without doing any changes to it… I’ll change the wait time and I hope it won’t fail again.