2017-09-01 60 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