Selenium Webdriver - 刷新陈旧元素快捷方式?
Selenium Webdriver - Refresh Stale Element Shortcut?
我正在为我的 Rails 应用程序编写一个 Selenium 测试,有时 运行 进入 "Stale Element" 异常。在对元素进行操作之前对其执行另一次查找很容易,但为了让我的代码保持干爽,我想知道 - Selenium::WebDriver::Element
对象上是否有任何可用的方法 "refresh" 过时引用?
编辑:为了澄清,我想问的是是否有一些 shorthand 方法可以执行另一个 find_by_xpath
(或 id
等)调用,并传递相同的参数。它功能完善,只是不像某种 "refresh" 方法那样简洁。
唯一可用的是在编写 Selenium 代码时跟踪您的应用程序状态。
您是否执行了刷新 DOM 的操作?然后您需要执行另一个 findElement
调用以获取您接下来要与之交互的元素的新实例。
否则,在尝试查找元素的实用程序 class 中编写您自己的方法 findElementByXpath
,捕获 StaleElement 异常并重试。
如果我理解正确,您是在询问 WebElement
实例上的某种 "refresh" 或 "update" 方法。
不幸的是,根据定义,selenium
中没有这样的东西。根据相关问题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.
我建议存储特定于元素的定位器,但是一旦页面刷新,您仍然需要通过定位器"refind" 元素。
我正在为我的 Rails 应用程序编写一个 Selenium 测试,有时 运行 进入 "Stale Element" 异常。在对元素进行操作之前对其执行另一次查找很容易,但为了让我的代码保持干爽,我想知道 - Selenium::WebDriver::Element
对象上是否有任何可用的方法 "refresh" 过时引用?
编辑:为了澄清,我想问的是是否有一些 shorthand 方法可以执行另一个 find_by_xpath
(或 id
等)调用,并传递相同的参数。它功能完善,只是不像某种 "refresh" 方法那样简洁。
唯一可用的是在编写 Selenium 代码时跟踪您的应用程序状态。
您是否执行了刷新 DOM 的操作?然后您需要执行另一个 findElement
调用以获取您接下来要与之交互的元素的新实例。
否则,在尝试查找元素的实用程序 class 中编写您自己的方法 findElementByXpath
,捕获 StaleElement 异常并重试。
如果我理解正确,您是在询问 WebElement
实例上的某种 "refresh" 或 "update" 方法。
不幸的是,根据定义,selenium
中没有这样的东西。根据相关问题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.
我建议存储特定于元素的定位器,但是一旦页面刷新,您仍然需要通过定位器"refind" 元素。