Reputation: 341
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
Upvotes: 0
Views: 1002
Reputation: 50899
Every time the DOM is changed, or even just refreshed, all previously located elements become stale and are no longer valid. It doesn't matter if the element looks identical, its a new element.
Java ExpectedConditions
(and only Java currently) have the refreshed to wait for the element to be redrawn in the DOM
(new WebDriverWait(driver, 30)).until(ExpectedConditions.refreshed(ExpectedConditions.visibilityOf(successMsg)));
Upvotes: 6