Java Selenium WebDriver 中的“缓存中未找到元素”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19310888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 15:58:18  来源:igfitidea点击:

"Element not found in the cache" in Selenium WebDriver

javaselenium-webdriverbrowser-cache

提问by Amith

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 :

有没有办法使用 Selenium WebDriver 清除浏览器缓存?如果我可以清除较大测试用例的缓存会有所帮助,有时我会收到以下异常:

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

我正在使用Java。任何建议将不胜感激,谢谢!

采纳答案by mentallurg

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:

该问题与浏览器缓存无关。这意味着您对浏览器 DOM 中的对象有一个过时的引用。通常可能有以下原因:

  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.

  1. 您可以在一页上找到一个元素。然后在浏览器中导航到另一个页面。然后在 Java 代码中,您尝试使用第一页中的元素。它不再有效,您将收到带有消息“缓存中未找到元素...”的 StaleElementReferenceException

  2. 您可以在一页上找到一个元素。然后使用 JavaScript 修改页面,您的元素从 DOM 中删除。即使创建了具有相同 ID 和样式的新元素,它也是一个新实例,并且 JavaCode 中的引用已过时。你需要再次查找这个元素,你会得到正确的引用。

回答by Sathish D

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 FFand Chromebecause a new profile is created on each launch.

目前,无法通过 Web 驱动程序 API 清除缓存。无论如何,如果您每次都可以启动浏览器的新实例,则应在FFChrome 中清除缓存,因为每次启动时都会创建一个新配置文件。

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:

如果无法创建新的浏览器实例,请查看Selenium 问题跟踪器中关于问题 #40(清除缓存)的评论,列出了您的问题的两个潜在解决方案:

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

1) 从命令行清除 IE 缓存 2) 使用自定义配置文件禁用 FF 缓存

回答by Ripon Al Wasim

The following code should work:

以下代码应该可以工作:

driver.manage().deleteAllCookies();

回答by danggrianto

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.

您的问题不在于清除缓存,而很可能在您尝试与元素交互时,您正在使用的元素已更改。这有时会发生在动态页面上,页面上的元素变化很快,或者您尝试重新使用该元素。在使用它之前尝试获取元素。

回答by Avinash G V

IE Browser

IE浏览器

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

每次页面加载后清除每个元素的缓存 ieCapabilities.setCapability(InternetExplorerDriver.ENABLE_ELEMENT_CACHE_CLEANUP, true);

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

这将进行会话清理。ieCapabilities.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);

回答by Prajeeth Anand

You can use explicit wait just before performing any action on that particular element,

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

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