1

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.

  1. I go to the Account Page
  2. Update Address Fields and click Update Address button,
  3. Refresh the Browser,
  4. 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>(); 
        }
    }
}
Richard C
  • 513
  • 1
  • 7
  • 26
  • Did you try putting some wait methods before the element ( **Update** button here ) ? – Chandra Shekhar Aug 09 '17 at 09:34
  • Possible duplicate of [StaleElementReference Exception in PageFactory](https://stackoverflow.com/questions/44838538/staleelementreference-exception-in-pagefactory) – undetected Selenium Aug 09 '17 at 09:37
  • While https://stackoverflow.com/questions/44838538/staleelementreference-exception-in-pagefactory is a similair question non of the solutions shown on that page are working for me. – Richard C Aug 09 '17 at 10:04
  • @Chandra Shekhar, looks like you might be right, after playing around with my breakpoints I have managed to get the test to pass while debugging. will have a play with sleep to see if I can get the test passing – Richard C Aug 09 '17 at 10:45
  • Once you `Clean up by reentering the original Address Data.` next when you `try to click the Update Address button` is the button present on the `HTML DOM`? – undetected Selenium Aug 09 '17 at 11:34

1 Answers1

0

Try this basic stuff

public static void ClickOnIt(this IWebElement element, string elementName)
{
    Thread.sleep(3000); // sleep for 3 seconds
    element.Click();
    Console.WriteLine("Clicked on " + elementName);
}

Then use Expicit wait instead of sleep method as it is not recommended .

Chandra Shekhar
  • 664
  • 2
  • 9
  • 24
  • Adding thread sleep has worked, however added it before update details.click as clickonit is used all over my framework and didnt want to add time to each test. Also managed to get it down to Thread.Sleep(75); will have a look and see if there is a nicer way of resolving this without using thread sleep as I really don't like using it throughout my testing – Richard C Aug 09 '17 at 12:35