0

I have a class called Navigation with simple functionality for navigating the pages of a website.

    public class Navigation
{
    public IWebElement Inner { get; set; }

    private IWebElement Next => Inner.FindElement(By.ClassName("Next"));

    public void NextPage() => Next.Click();
}

The above Navigation appears on every page so I put it in a BasePage class (as per the Page Object Model)

    public class BasePage
{
    protected IWebDriver driver;

    public Navigation navigation => new Navigation { Inner = driver.FindElement(By.ClassName("footer"))};
}

In my test class I have the following

public class Test 
    {
      BasePage basepage = new BasePage();

      basepage.navigation.NextPage();
      basepage.navigation.NextPage(); //NOT WORKING
    }

Clicking the Next button works the first time but fails the 2nd time. I get a StaleElementReferenceException on the NextPage() line of my Navigation class. I get why it's happening - I've navigated to a new page and as such both the footer and next elements are now old.

I could just find the Next button again from within the Navigation class, but I want for the Navigation class to remain inside the scope of the footer element (i.e. I don't want to do a driver.FindElement(By.ClassName("Next")) and search the entire page again for the Next button)

Is there a clean way in which I can find the footer element again? Ideally from within the BasePage class?

Thanks.

Keegan
  • 3
  • 4
  • that means you need to reconstruct the page objects again. Create a new instance of BasePage after your next page first time and check. This thread might help you https://stackoverflow.com/questions/45331708/staleelementexceptionselenium-how-do-i-handle-this/45331785#45331785 – santhosh kumar Apr 13 '18 at 06:23
  • 1
    Possible duplicate of [StaleElementReference Exception in PageFactory](https://stackoverflow.com/questions/44838538/staleelementreference-exception-in-pagefactory) – undetected Selenium Apr 13 '18 at 07:48
  • Thanks - I was over-thinking the issue. Creating a new instance of BasePage is a simple and easy way of which solving the problem. – Keegan Apr 16 '18 at 23:29

0 Answers0