PageFactory 中的 StaleElementReference 异常 [英] StaleElementReference Exception in PageFactory
问题描述
我正在尝试学习 PageFactory 模型.我理解当我们执行 initElements
时,WebElements 被定位的事实.例如,我点击了一个 webelement,因此 DOM 中的其他 webelement 之一发生了变化.现在,显然我会在这里得到一个 StaleElementReferenceException
.我将如何解决这个问题?
I am trying to learn the PageFactory model. I understood the fact that when we do a initElements
, the WebElements are located. Say for example, I click on a webelement and because of which there is a change in one of the other webelements in DOM. Now, obviously I would get a StaleElementReferenceException
here. How would I resolve this issue?
如果知道 DOM 中 WebElement 的属性可能会发生变化这一事实,我是否应该再次找到该特定 WebElement?或者有其他方法可以解决这个问题吗?
Should I find that specific WebElement again knowing the fact that there can be a change in the WebElement's properties in the DOM? or is there an another way to handle this?
推荐答案
StaleElementReferenceException
StaleElementReferenceException 扩展
StaleElementReferenceException
StaleElementReferenceException extends WebDriverException and indicates that the previous reference of the element is now stale and the element reference is no longer present on the DOM of the page.
- 面临
StaleElementReferenceException
的常见原因如下:- 该元素已被完全删除.
- 该元素不再附加到 DOM.
- 该元素所在的网页已刷新.
- (前一个)元素已被 JavaScript 或 AjaxCall 删除,并被具有相同
ID 的(新)元素替换
或其他属性.
- The common reasons behind facing
StaleElementReferenceException
are as follows:- The element has been deleted entirely.
- The element is no longer attached to the DOM.
- The webpage on which the element was part of has been refreshed.
- The (previous) element has been deleted by a JavaScript or AjaxCall and is replaced by a (new) element with the same
ID
or other attributes.
当我们做一个initElements时,WebElements位于:当你调用
initElements()
方法时,所有的WebElements该页面将被初始化.例如,
When we do a initElements, the WebElements are located : When you call
initElements()
method, all the WebElements of that page will get initialized. For example,
LoginPageNew login_page = PageFactory.initElements(driver, LoginPageNew.class);
这行代码将初始化在
LoginPageNew.class
范围内定义的所有静态 WebElements,无论何时何地从您的自动化脚本调用.This line of code will initialize all the static WebElements defined within the scope of the
LoginPageNew.class
whenever and wherever it is invoked from your Automation Script.我点击了一个 webelement,因此 DOM 中的其他 webelement 中的一个发生了变化:这很有可能.
- 例如,通常在
标签上调用
click()
不会触发任何 WebElementsem> 在 HTML DOM 上. - 在
标签或
标签上调用
click()
可能会调用 JavaScript 或 Ajax 反过来可以删除一个元素或可以用具有相同ID
或其他属性的(新)元素替换(前一个)元素.
- As an example, in general invoking
click()
on a<input>
tag wouldn't trigger any change of any of the WebElements on the HTML DOM. - Where as invoking
click()
on a<button>
tag or<a>
tag may call a JavaScript or a Ajax which inturn may delete an element or can replace the (previous) element by a (new) element with the sameID
or other attributes.
<小时>
结论
因此,如果 WebDriver 抛出 StaleElementReferenceException,这意味着即使元素仍然存在,引用也会丢失.我们应该丢弃我们拥有的当前引用,并在 WebElement 附加到 DOM 时再次定位它来替换它.这意味着您必须再次通过
initElements()
方法重新初始化该类,该方法又重新初始化该页面中定义的所有 WebElements.
Conclusion
So, if WebDriver throws a StaleElementReferenceException, that implies even though the element still exists, the reference is lost. We should discard the current reference we have and replace it by locating the WebElement once again when it gets attached to the DOM. That means you have to again reinitialize the class through
initElements()
method which inturn reinitializes all the WebElements defined in that page.如果旧元素已被替换为相同的新元素,简单的策略是调用 WebDriverWait 与 ExpectedConditions 以查找元素.
If a old element has been replaced with new identical one, the simple strategy would be to invoke WebDriverWait inconjunction with ExpectedConditions to look out for the element.
您可以在以下位置找到相关的详细讨论:
You can find relevant detailed discussions in:
以下是本次讨论的参考资料:
Here are the references of this discussion:
- 陈旧元素引用异常
- 类 StaleElementReferenceException莉>
- Selenium:如何判断 RemoteWebDriver.findElements(By) 可以抛出 StaleElementReferenceException 吗?
这篇关于PageFactory 中的 StaleElementReference 异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!