jessica
jessica

Reputation: 1517

Element not found in the cache - perhaps the page has changed since it has looked up

I have a set of tests in which the first one runs and rest fails. Its just adding a list of items to cart.I have an error: "Element not found in the cache - perhaps the page has changed since it has looked up". I tried using the below code which doesnt seem to help.

driver.Manage().Cookies.DeleteAllCookies();

Is there any way to clear the cache or get rid of this error.

Code: It stops in this verify method. When I comment out these lines the test runs for all items.

    public bool VerifyItemPresentInCart()
    {
            //Get the cartsize and verify if one item present
            IWebElement cartSize = driver.FindElement(By.CssSelector("div[class='cart-size']>div"));                                             
            string actualMsg = cartSize.Text;
            string expectedMsg = "1";
            VerifyIfTextPresentMethod(expectedMsg,actualMsg);                
            return true;                  
    }

Update: The test has common methods and so methods repeat for each item to be added in cart. this is one of those common methods. these methods work for the first item say a phone and adds it to the cart. For the second item when the whole process is repeated i get this error in this method.

Upvotes: 1

Views: 2579

Answers (1)

Amey
Amey

Reputation: 8548

The error you see is because of one of the two reasons

  1. The element has been deleted entirely.
  2. The element is no longer attached to the DOM.

The webelement you are trying to interact with, cannot be "referenced" again, after navigating away from the page.

Check the line in your code where this error occurs, and try to lookup the element again.

Example : -

webelement = @driver.find_element(:id, "amey")
# Some other interaction whcih causes the element to become "stale"
webelement.send_keys "Hey!" # "Element not found in the cache - perhaps the page has changed since it has looked up" error is shown

change it to

@driver.find_element(:id, "amey").send_keys "Hey!" #lookup the same element again

For more info look up here

UPDATE

Made changes to your code

{
            //Get the cartsize and verify if one item present
            VerifyIfTextPresentMethod("1",driver.FindElement(By.CssSelector("div[class='cart-size']>div")).Text);                
            return true;                  
}

Upvotes: 0

Related Questions