We have a UI framework with Page Object and Page Factory design patterns. In one of my Page Object classes I have defined a webelement and calling it. In my test step class, I am calling this webelement once when this is on one page and I am calling this webelement again when this is on another page. Below is how my code looks like
PageObjectClass1:
@FindBy(how=How.XPATH, using="//*[contains(text(),'Successfully')]")
@CacheLookup
public WebElement successMsg;
testStepClass:
//on first page
PageObjectClass1.successMsg.isDisplayed()
//Then I navigate to some other page and again call this webelement
PageObjectClass1.successMsg.isDisplayed();
While calling it the second time around webdriver is giving me staleElementException. This xpath is generic xpath and would be used in every page. This definitely is present in the DOM so not sure why it is giving me this exception ? How can I use it without creating this webelement in every page object class ?
Exception given is :
org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document