2013-05-29 22 views
1

我有一組測試,其中第一個運行並且其餘失敗。它只是將一個項目列表添加到cart.I有一個錯誤:「在緩存中找不到元素 - 可能頁面已經查找,因爲它已經查找」。 我嘗試使用下面的代碼,似乎沒有幫助。在緩存中未找到元素 - 可能頁面已經更改,因爲它已查找到

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

更新:測試有常用的方法,所以方法重複每個項目添加到購物車。這是這些常用方法之一。這些方法適用於第一個項目,說一個電話,並將其添加到購物車。對於整個過程重複的第二項,我在這個方法中得到這個錯誤。

回答

0

您所看到的錯誤是因爲兩個原因之一

  1. 的元素已經被全部刪除。
  2. 該元素不再附加到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 

欲瞭解更多信息查找here

UPDATE

更改了代碼

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

所以我應該從driver.FindElement(By.CssSelector(「div [class ='cart-checkout-content']> button」))更改Click(); to @ driver.FindElement(By.CssSelector(「div [class ='cart-checkout-content']> button」))。Click(); – jessica

+0

不要關注'@',我的代碼示例在Ruby中。重點在於如果它變得陳舊,就再次查找相同的元素。您可以更新顯示錯誤的代碼部分,我們可以看看它。 – Amey

+0

我已經更新了代碼停止的地方 – jessica

相關問題