Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在缓存中找不到元素-可能页在查找后已更改_C#_Selenium_Selenium Webdriver - Fatal编程技术网

C# 在缓存中找不到元素-可能页在查找后已更改

C# 在缓存中找不到元素-可能页在查找后已更改,c#,selenium,selenium-webdriver,C#,Selenium,Selenium Webdriver,我有一组测试,其中第一个测试运行,其余测试失败。它只是向购物车添加了一个项目列表。我有一个错误:“在缓存中找不到元素-可能页面在查找后已更改”。 我尝试使用下面的代码,但似乎没有帮助 driver.Manage().Cookies.DeleteAllCookies(); 是否有任何方法可以清除缓存或消除此错误 代码:它在此验证方法中停止。当我注释掉这些行时,测试将针对所有项目运行 public bool VerifyItemPresentInCart() {

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

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

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

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

  • 该元素已被完全删除
  • 元素不再附加到DOM
  • 您尝试与之交互的webelement在离开页面后无法再次“引用”

    检查代码中发生此错误的行,然后再次尝试查找该元素

    示例:-

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

    那么我是否应该从driver.findelelement(By.CssSelector(“div[class='cart-checkout-content']>按钮”))更改;到@driver.FindElement(通过.CssSelector(“div[class='cart-checkout-content']>按钮”)。单击();不,不是关于
    @
    ,我的代码示例是Ruby。关键是在同一个元素过时时再次查找它。您可以更新出现错误的代码部分,我们可以查看一下。我的等待方法有问题吗。我有两种不同的等待方法。。一个是隐式的,另一个是显式的。已使用等待更新代码。当您打印cart-checkout.text时,您得到了什么?第一次它将预期消息打印为1(如在程序中),第二次测试它停止并显示此错误。因此,我认为它仅在验证方法中停止。如果它没有点击checkout,那么它应该为第二个项目打印相同的消息“1”,但它没有。