元素未在缓存中找到-可能页面在查找后发生了更改

本文关键字:查找 发生了 缓存 元素 | 更新日期: 2023-09-27 18:16:00

我有一组测试,其中第一个运行,其余的失败。它只是将商品列表添加到购物车中。我有一个错误:"在缓存中没有找到元素-也许页面已经更改,因为它已经查找"。我试着使用下面的代码,但似乎没有帮助。

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

是否有办法清除缓存或摆脱这个错误。

代码:它在这个验证方法中停止。当我注释掉这些行后,测试将运行所有项。

    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:该测试具有公共方法,因此对于要添加到购物车中的每个项目,方法重复。这是一种常见的方法。这些方法适用于第一件商品,比如手机,并将其添加到购物车中。对于第二个项目,当整个过程重复时,我在这个方法中得到这个错误。

元素未在缓存中找到-可能页面在查找后发生了更改

您看到的错误是由于以下两个原因之一

  1. 元素已被完全删除。
  2. 该元素不再附加到DOM。

您尝试与之交互的web元素,在离开页面后不能再被"引用"。

检查代码中出现此错误的行,并尝试再次查找该元素。

示例:-

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

改为

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

更多信息请查看此处

修改你的代码

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