我正在为我的 Rails 应用程序编写 Selenium 测试,有时会遇到“陈旧元素”异常。在对元素进行操作之前对其执行另一次查找很容易,但为了让我的代码保持干爽,我想知道 - Selenium::WebDriver::Element
对象是否有任何可用的方法“刷新”过时的引用?
编辑:为了澄清,我想问的是是否有一些速记方法可以使用传递的相同参数执行另一个 find_by_xpath
(或 id
等)调用。它功能完善,只是不像某种“刷新”方法那样简洁。
最佳答案
如果我理解正确,您是在询问 WebElement
实例上的某种“刷新”或“更新”方法。
不幸的是,根据定义,selenium
中没有这样的东西。根据相关issue WebElement refresh() or getBy() needed :
WebElement object is a proxy, a representative of a DOM element. When DOM element is destroyed by the browser, WebElement is marked "stale" and can't be used anymore.
If you have a similar DOM element on another page, you can obtain it with findElement, and Selenium will create a new WebElement object (a proxy) for this new DOM element. Your old WebElement object will remain stale because underlaying DOM object is destroyed and never can be restored. Similarity is not equality.
我建议存储特定于元素的定位器,但是一旦页面刷新,您仍然需要通过定位器“重新查找”该元素。
关于ruby-on-rails - Selenium Webdriver - 刷新陈旧元素快捷方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29353528/