PageFactory中的StaleElementReference异常 [英] StaleElementReference Exception in PageFactory
问题描述
我正在尝试学习PageFactory模型.我了解以下事实:当我们执行initElements
时,将定位WebElement.举例来说,我单击一个Web元素,因此DOM中的其他Web元素之一发生了变化.现在,显然我会在这里得到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?
我是否应该再次知道特定的WebElement知道DOM中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 扩展了 WebDriverException 并指出元素现在陈旧,并且元素引用在页面DOM上不再存在.
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.我单击一个Web元素,因此DOM中的其他Web元素之一发生了变化:这几乎是可能的.
I click on a webelement and because of which there is a change in one of the other webelements in DOM : This is pretty much possible.
- 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.如果将旧元素替换为新的相同元素,则简单的策略是调用 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:
以下是此讨论的参考:
- 陈旧元素引用异常
- StaleElementReferenceException类
- Selenium:如何判断RemoteWebDriver.findElements (按)可以完全抛出StaleElementReferenceException吗?
- Stale Element Reference Exception
- Class StaleElementReferenceException
- Selenium: How to tell if RemoteWebDriver.findElements(By) can throw StaleElementReferenceException at all?
这篇关于PageFactory中的StaleElementReference异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!