I had already looked at StaleElementReference Exception in PageFactory before setting this question and none of the answers there seemed to resolve my issue.
I am having problems resolving the error message
OpenQA.Selenium.StaleElementReferenceException : stale element reference: element is not attached to the page document.
I know exactly what is causing the issue but cant find a way to resolve it.
Code below but as an overview.
- I go to the Account Page
- Update Address Fields and click Update Address button,
- Refresh the Browser,
- Confirm the Address has been updated.
I then Clean up by reentering the original Address Data. However when I try to click the Update Address button it is showing as no longer attached to the page (although it is visible). I am using the Page Factory Model as my framework. I understand that to resolve the error I need to find the element again but I can't work out how to do this. Whatever method I use I need to be able to apply it across my whole framework as I have a gut feeling it is going to crop up repeatedly on the site I am testing.
My Code
test throwing the error.
[Test]
public void Change_Account_Address()
{
Page.headerView.ClickOnLogin();
Page.loginPage.EnterUserNameandPasword(_testName);
Page.accountPage.ConfirmAtAccountPage(_testName);
Page.accountPage.UpdateAccountAddress(_testName);
Browsers.Refresh();
Page.accountPage.ConfirmAddressisUpdated(_testName);
Page.accountPage.UpdateAccountAddress("ResetAccountAddress"); - Error occurs on this step.
Page.accountPage.LogOut();
}
Account page button that is causing the error
[FindsBy(How = How.CssSelector, Using = "#myAccountForm > div:nth-child(3) > button")]
[CacheLookup]
private IWebElement UpdateDetails { get; set; }
Using that button in the test
public void UpdateAccountAddress(string testName)
{
var testData = ExcelDataAccess.GetTestData(testName);
AddressLine1.EnterText(testData.AddressLine1, "AddressLine1");
AddressLine2.EnterText(testData.AddressLine2, "AddressLine2");
AddressLine3.EnterText(testData.AddressLine3, "AddressLine3");
City.EnterText(testData.City, "City");
AccountPostcode.EnterText(testData.AccountPostcode, "AccountPostcode");
UpdateDetails.ClickOnIt("UpdateButton");
}
Click on it Extension
public static void ClickOnIt(this IWebElement element, string elementName)
{
element.Click();
Console.WriteLine("Clicked on " + elementName);
}
Finally my Page Class
public class Page
{
private static T GetPage<T>() where T : new()
{
var page = new T();
PageFactory.InitElements(Browsers.getDriver, page);
return page;
}
public static AccountPage accountPage
{
get
{
return GetPage<AccountPage>();
}
}
}