简体   繁体   中英

“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!

Presently, there is no way to clear cache through the web driver API. Anyway, if you can start a new instance of the browser each time, the cache should be cleared in FF and Chrome because a new profile is created on each launch.

Please check the comments for issue #40 (Clear cache) in the Selenium issue tracker list two potential solutions to your problem if creating a new browser instance isn't possible:

1) Clear the IE cache from the command line 2) Disable the FF cache using a custom profile

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.

Your problem is not about clearing the cache but most likely when you try to interact with the element the element that you are using is already changed. This is sometimes happen on the dynamic page that the elements on the page changes quickly, or maybe you try to re-use the element. Try to get the element before using it.

您可以在对该特定元素执行任何操作之前使用显式等待,

new WebDriverWait(driver,10).until(ExpectedConditions.presenceOfAllElements("Your object property"));

IE Browser

Clears the cache for each element after every page load ieCapabilities.setCapability(InternetExplorerDriver.ENABLE_ELEMENT_CACHE_CLEANUP, true);

This will do session clean up. ieCapabilities.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);

以下代码应该可以工作:

driver.manage().deleteAllCookies();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM