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 web
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
are as follows:
ID
or other attributes.findElement()
or findElements
to look out for the element again.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);
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.
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.
click()
on a
tag wouldn't trigger any change of any of the WebElements on the HTML DOM. click()
on a
tag or
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 same ID
or other attributes.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.
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: