2017-09-01 100 views
0

我必須點擊頁面上的某個按鈕。但是,當我檢索具有特定類名稱的所有元素時。當我嘗試執行每一個或單擊時,所有檢索到的元素都會拋出一個陳舊的引用異常。我不能雙擊任何一個。它找到了正確的元素,但卻引發了所有這些異常。註釋掉的代碼是我實際嘗試選擇的位置,然後單擊相應的按鈕。我附上了表格的圖片。請注意,每次單擊按鈕或執行按鈕時頁面都會更改。選擇上傳BOM按鈕是您需要特別注意的。 Website硒網絡驅動程序陳舊引用異常

// Switch to correct frame 
     IWebElement editorFrame = driver.FindElement(By.ClassName("frame-banner")); 
     driver.SwitchTo().Frame(editorFrame); 
     var action = new OpenQA.Selenium.Interactions.Actions(driver); 
     // Select Project File 
     IList<IWebElement> projectFileButtonList= driver.FindElements(By.ClassName("data-cell")); 
     foreach (var button in projectFileButtonList) 
     { 
      if (button.Text == "BOM_scrub") 
      { 
       // Found Project File now select it 
       action.DoubleClick(button); 
       action.Perform(); 
       break; 
      } 
     } 
     // Select Upload BOM Button 
     IList<IWebElement> uploadBomBtn = driver.FindElements(By.ClassName("se-custom-main-button")); 
     foreach (var element in uploadBomBtn) 
     { 
      try 
      { 
       action.DoubleClick(element); 
       action.Perform(); 
      } 
      catch 
      { 

      } 
      /* 
      if (element.Text == "Upload BOM") 
      { 
       int i = 0; 
       while (i == 0) 
       { 
        try 
        { 
         action.DoubleClick(element); 
         action.Perform(); 
         break; 
        } 
        catch 
        { 

        } 
       } 

      } 
      */ 
     } 
+0

https://stackoverflow.com/questions/45434381/stale-object-reference-while-navigation-using-selenium/45435158#45435158 –

+1

[PageFactory中的StaleElementReference異常]的可能重複(https://stackoverflow.com/questions/44838538/staleelementreference-exception-in-pagefactory) – DebanjanB

回答

0

不要使用動態組件使用driver.findElement(-s)

StaleElementReferenceException發生時,你要執行對元素,它已經從DOM分離動作。

你必須使用顯式的等待機制(+ ExpectedConditionsWebDriverWait的組合),它會自動刷新元素的狀態,並返回其有效表示,當滿足指定條件。

+0

你能舉個例子嗎?我找不到WebDriverWait,我只有WebDriverException和WebDriverTimeOutException。 – weknowgp

+0

@weknowgp看到https://seleniumhq.github.io/selenium/docs/api/dotnet/html/T_OpenQA_Selenium_Support_UI_WebDriverWait.htm和官方文檔http://www.seleniumhq.org/docs/04_webdriver_advanced.jsp舉例。 。 –

+0

wait.Until(DRV => drv.FindElements(By.ClassName( 「SE-定製主鍵」))[9])點擊(); – weknowgp