Search code examples
javaselenium-webdriverbrowser-cache

"Element not found in the cache" in Selenium WebDriver


Is there any way to clear browser cache using Selenium WebDriver? It would help if I can clear cache for larger test cases, sometimes I get the following exception :

"Element not found in the cache - perhaps the page has changed since it was looked up".

I'm using java. Any suggestions would be appreciated, thanks!


Solution

  • The problem has nothing to do with browser cache. It means that you have a stale reference to an object in the Browser DOM. Typically there can be following reasons:

    1. You find an element on one page. Then navigate in browser to another page. Then in Java code you attempt to use the element from the 1st page. It is not valid any more, and you will get a StaleElementReferenceException with the message "Element not found in the cache ..."

    2. You find an element on one page. Then the page is being modified with JavaScript, your element is removed from DOM. Even if a new element with the same ID and styles is created, it is a new instance, and reference in your JavaCode is stale. You need to look up this element again, and you will get the correct reference.