StaleElementReference Exceptions in Selenium Webdriver

This Exception occurs when driver is trying to perform action on the element which is no longer exists or not valid.

WebElement ele = driver.findElement("sample"));
// Before clicking some thing happened and DOM has changed due to page refresh, or element is removed and re-added;

Now at this point, the element which you're clicking is no longer valid.

so it throws up its hands and gives control to user, who as the test/app author should know exactly what may or may not happen.

In order overcome this, we need to explicitly wait until the DOM is in a state where we are sure that DOM won't change.

For example, using a WebDriverWait to wait for a specific element to exist:

/ times out after 10 seconds
WebDriverWait wait = new WebDriverWait(driver, 10);

When Javascript / Ajax updates the page between the findElement and the click call then will get a StaleElementException. Here the reference to the element in the DOM that previously had becomes stale and we can no longer able to use this reference (click call) to interact with the element in the DOM.

Try to get around this by first using an explicit wait on the element to ensure the ajax call is complete, then get a reference to the element again.

// while the following loop runs, the DOM changes or page refreshed, or element removed and re-added

//Or like
By byPageLoc ="element");

// now try to click the element
private static Function<WebDriver,WebElement> elementIdentified(final By locator) {
    return new Function<WebDriver, WebElement>() {
        public WebElement apply(WebDriver driver) {
            return driver.findElement(locator);

You can also user implicit waits, in which Webdriver will check for the element to become present until the specified timeout:

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS)

If even this is not working for you, try giving an other locator which gives some positive results. And in the last if nothing works out, just retrying it for second time would work sometimes. This solution is not recommended because of time taken for each iteration that fails to find the element and end up wasting time in retry.

Selenium Tutorials: 


Hi ,

I trying to integrate webdriver with PhantomJSDriver for headless browser automation but its not working properly. Getting error.

java.lang.IllegalStateException: The path to the driver executable must be set by the phantomjs.binary.path capability/system property/PATH variable; for more information, see The latest version can be downloaded from
at org.openqa.selenium.phantomjs.PhantomJSDriverService.findPhantomJS(
at org.openqa.selenium.phantomjs.PhantomJSDriverService.createDefaultService(

Your help much appreciated.


I think you need to add the path of your phantom to your class path. check how to configure webdriver. You need to open the environmental variable and update the path.

Use ' try -- catch' in for loop for Java, ' try -- except' in for loop for Python

Hi I have one label after clicking on it it is dispalying list of image alnog with the iteam example its displying salad and asking user to select list of the salad displying in the list of the images ,dispalying pie and asking select list of the items size like pie likewiese the label and images are getting change after every click if user will select the label displyed list of images user will able to click on verify button and page will redirect to the next if not the label will refresh to new item along with list of the images.please let me know how to handle this selenium web driver

I have tried explicit wait as well as the above code but still getting the same error :
Error :
"Element not found in the cache - perhaps the page has changed since it was looked up
Command duration or timeout: 120.26 seconds"

It works some times but not always. tried with more wait time too .
Is there any other way to handle this ?

It depends on browser.

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.