ruby-on-rails - Selenium Webdriver - 刷新陈旧元素快捷方式?

标签 ruby-on-rails ruby selenium selenium-webdriver

我正在为我的 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/

相关文章:

ruby-on-rails - ActionController::UnfilteredParameters - 无法将未经允许的参数转换为散列 - Rails 5.1.6

mysql - 运行时"Sphinx cannot be found on your system.""rake ts:index"

python - 多台主机上的并行 rsync

c++ - 语句 "a class is an object even if not instantiated"是什么意思?

javascript - 当 Selenium 中自动下拉菜单时,无法触发 Angular ng-change 功能

ruby-on-rails - 如何在具有名为 'attribute' 的列的数据库上使用 ActiveRecord ? (危险属性错误)

mysql - 删除ruby用户记录

ruby-on-rails - 在 Rails 中迭代嵌套对象的最佳方法是什么

python - 如何在每个请求上使用 selenium python 轮换各种用户代理

testing - 如何开始使用 Selenium Webdriver?