【问题标题】:Element not found in the cache - perhaps the page has changed since it has looked up在缓存中找不到元素 - 页面可能在查找后已更改
【发布时间】:2013-05-29 17:32:54
【问题描述】:

我有一组测试,其中第一个运行并且休息失败。它只是将项目列表添加到购物车。我有一个错误:“在缓存中找不到元素 - 页面可能在查找后已更改”。 我尝试使用以下似乎没有帮助的代码。

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;                  
    }

更新:测试具有通用方法,因此对于要添加到购物车中的每个项目重复方法。这是那些常见的方法之一。这些方法适用于第一个项目,比如电话并将其添加到购物车。对于第二个项目,当整个过程重复时,我在这个方法中得到了这个错误。

【问题讨论】:

    标签: c# selenium selenium-webdriver


    【解决方案1】:

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

    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
    

    更多信息请查看here

    更新

    对您的代码进行了更改

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

    【讨论】:

    • 所以我应该从 driver.FindElement(By.CssSelector("div[class='cart-checkout-content']>button")).Click();到@driver.FindElement(By.CssSelector("div[class='cart-checkout-content']>button")).Click();
    • 不,这与@ 无关,我的代码示例是用Ruby 编写的。关键是如果它变得陈旧,则再次查找相同的元素。您可以更新出现错误的代码部分,我们可以查看一下。
    • 我的等待方法有问题吗?我有两种不同的等待方法。一种是隐式的,另一种是显式的。已通过等待更新了代码。
    • 打印 cart-checkout.text 会得到什么?
    • 第一次将 Expected msg 打印为 1(如在程序中),第二次测试它停止并显示此错误。所以我认为它只在验证方法中停止。如果它未能点击结帐,那么它应该为第二个项目打印相同的消息“1”,但它没有。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-16
    • 1970-01-01
    相关资源
    最近更新 更多