StaleElementReferenceException in selenium

Exception:

In simple words when we are expecting something to happen but in reality, if some other thing happens then it is an exception.

Let's say, you are adding a number two with two, now the obvious answer should be 4 but due to some reasons if you receive 6 as the answer, then it is an exception.

An exception can happen due to various reasons, based on the reason we will call the exception as a specific type of exception.

How any operation works:

Before I can teach you about the StaleElementReferenceException; I want you guys to understand how any operation works in Selenium.

For example, let's take an element and you want to click it.. So for clicking an element there will be three steps.

  • Find the element
  • Find the element address
  • Perform the action on the element (here click the element).

The above three steps will not happen parallel, post finding the element it will take some time to get the address of the element, post finding the element's address, Selenium will take little time to perform the action.

Selenium cannot find an element then it will throw an exception called no such element exception. Normally while finding the element address you may not see any exception. While performing the operation only you can see see the StaleElementReferenceException

StaleElementReferenceException

As explained earlier, each operation takes a little bit of time. After storing the element if the page gets refreshed or a particular section gets refreshed where the element is present then we will receive a StaleElementReferenceException.

When your page is loaded in the browser, all the elements present in the web page will be stored at addresses in the system. When we say we find the element in Selenium, it basically finds this address of the target element.

The StaleElementReferenceException occurs if the browser refreshes between finding the element's address and performing the action.
stale-element-reference-exception-process

Normally when a StaleElementReferenceException occurs people try to see whether the element is present or not; this is the wrong approach because the element is going to be present there. The only problem is the refresh that occurred before performing the action.

handle stale element exception in selenium

Now let's see how to handle the StaleElementReferenceException. If the refresh occurs in the browser then you can complain the same thing to the developer.

But if the refresh occurs because of the ajax or because some of the divisions get refreshed then the developer might have refreshing it intentionally.

For example, if you have seen graphs and charts, they will be kept on refreshing to add fresh data. In such cases, you will find more and more of a StaleElementReferenceException.

Example: Navigate to this page and try to click on the button which says refreshed after 10 seconds. Here, the button will be refreshed once in 10 seconds. So to create a stale element reference exception we might need to wait for 15 to 20 seconds using selenium before performing the operation.
stale-element-reference-exception

public static void main(String[] args) throws Exception {
	String driverPath = "D:\\Eclipse progs\\driverserver\\chromedriver.exe";
	System.setProperty("webdriver.chrome.driver", driverPath);
	WebDriver driver = new ChromeDriver();
	driver.get("https://chercher.tech/practice/stale-element");

	WebElement refreshButton = driver.findElement(By.id("refresh-button"));
	Thread.sleep(15000);
	// sleeping for 10 seconds so the element can refresh
	refreshButton.click();
}

The output:

Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
  (Session info: chrome=88.0.4324.104)
  (Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 10.0.18363 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
Way 1:

If you think you can handle it by just try..catch then it is not possible. Use for loop till you can achieve your goal along with try..catch, so got to try clicking again and again till there is no exception.

public static void main(String[] args) throws Exception {
	String driverPath = "D:\\Eclipse progs\\driverserver\\chromedriver.exe";
	System.setProperty("webdriver.chrome.driver", driverPath);
	WebDriver driver = new ChromeDriver();
	driver.get("https://chercher.tech/practice/stale-element");
	  
	for (int i = 0; i < 10; i++) {
		try {
		  driver.findElement(By.id("refresh-button")).click();
		  break;
		}catch (StaleElementReferenceException e) {
			// do nothing
		}
	} 
}

The problem with the above method, you cannot judge, how many times that you have to try.

Way 2:

In way 2, you just wait for the element to refresh using the WebdriverWait.

public static void main(String[] args) throws Exception {
	String driverPath = "D:\\Eclipse progs\\driverserver\\chromedriver.exe";
	System.setProperty("webdriver.chrome.driver", driverPath);
	WebDriver driver = new ChromeDriver();
	driver.get("https://chercher.tech/practice/stale-element");

	WebDriverWait wait = new WebDriverWait(driver, 30);
	WebElement refreshButton = driver.findElement(By.id("refresh-button"));
	wait.until(ExpectedConditions.stalenessOf(refreshButton));
	driver.findElement(By.id("refresh-button")).click();
}
Avoid stale element exception:
  • As a tester, you cannot avoid the stale element exception
  • Ask your developer at what interval he tries to refresh and code accordingly
  • Ask the developer to refresh the values than the elements.
About Author :

I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions