Question: The code below clicks on each button and then clicks on dynamic link produced by the button which takes me to next page however when test navigates back it fails to carry out the similar steps because of "Element not found in the cache - perhaps the page has changed since it was looked up". I need to test links and move backwards and forwards during the process.

Code to click and navigate

```
public static int ClickNestedLink(int ID, IList<string>allNestedlinks,IWebDriver _driver,string Url)
    {            
        //Find the product brand and click on its brand and click on product link
        foreach (string BrandName in allNestedlinks)
        {

            IList<string> ProductBrandButtonList = new List<string>();                 

            //Find different brands
            foreach (IWebElement ProductBrandButton in _driver.FindElements(By.ClassName("productSet")))
            {
                IWebElement ProductParent = ProductBrandButton.FindElement(By.ClassName("product-parent"));
                String test = ProductParent.GetAttribute("data-product-id");
                ProductBrandButtonList.Add(test);
                //Find links for a brand  
                IList<string> LinksCollection = new List<string>();

                foreach (IWebElement ImageLink in ProductParent.FindElements(By.ClassName("brand_image")))
                {                        
                    String ImageLike2 = ImageLink.GetAttribute("src");
                    LinksCollection.Add(ImageLike2);
                    ImageLink.Click();
                    IWebElement productBrandClick = ProductParent.FindElement(By.ClassName("product-brand"));
                    String Test2 = productBrandClick.Text;
                    productBrandClick.Click();
                    _driver.Navigate().Back();
                    Task.Delay(20000).Wait();                                            }
            }                
        }
        return (ID);
    }

```

Answer: 
The `Click` and `Back` actions are loading a new page with new elements making the previous references obsolete.
To overcome this issue, you need locate the elements in each loop:

```
// iterate the products
for (int i = 0; ; i++) {
    var products = _driver.FindElements(By.CssSelector(".productSet .product-parent"));
    if (i >= products.Count)
        break;

    // get the product for the iteration
    var product = products[i];

    // iterate the links
    for (int j = 0; ; j++) {
        var links = _driver.FindElements(By.CssSelector(".productSet .product-parent .brand_image"));
        var brands = _driver.FindElements(By.CssSelector(".productSet .product-parent .product-brand"));
        if (j >= links.Count)
            break;

        // get the link and brand for the iteration
        var link = links[j];
        var brand = brands[j];

        // click and navigate back
    }
}

```
Solution

public static int ClickNestedLink2(int ID, IList allNestedlinks, IWebDriver \_driver, string Url)
 {

```
        var products = _driver.FindElements(By.CssSelector(".productSet .product-parent"));
        var productCount = products.Count;

        for (int i = 0; i < productCount; i++)
        {
            var product = _driver.FindElements(By.CssSelector(".productSet .product-parent"))[i];
            var brandButtonsCount = product.FindElements(By.CssSelector(".productSet .product-parent .brand_image")).Count;

            for (int btnIdx = 0; btnIdx < brandButtonsCount; btnIdx++)
            {
                product = _driver.FindElements(By.CssSelector(".productSet .product-parent"))[i];
                var brandButton = product.FindElements(By.CssSelector(".productSet .product-parent .brand_image"))[btnIdx];

                brandButton.Click();

                var brandNameAnchor = product.FindElement(By.CssSelector(".productSet .product-parent .product-brand"));
                brandNameAnchor.Click();

                _driver.Navigate().Back();

                OpenQA.Selenium.Support.UI.WebDriverWait wait = new WebDriverWait(_driver, new TimeSpan(0, 0, 10));
                wait.Until(driver1 => ((IJavaScriptExecutor)_driver).ExecuteScript("return document.readyState").Equals("complete"));

            }
        }
        return (ID = 0);
    }        
}

```