2015-07-21 69 views
-1

我在嘗試執行拖放操作時看到StateElementReferenceException,請參閱下面的代碼片段。任何人都可以請幫忙解決這個適當的建議和解釋?Selenium中的StateElementReferenceException

@Test(priority=4) 
public void addUserBySearch() { 
    driver.findElement(By.xpath(OR.getProperty("btnUserGroup"))).click(); 
    driver.findElement(By.xpath(OR.getProperty("btnCreateUG"))).click(); 
    driver.findElement(By.xpath(OR.getProperty("textUGName"))).sendKeys("UserGroup:" + Utils.randGen()); 
    driver.findElement(By.xpath(OR.getProperty("userSearchField"))).sendKeys("testuser2", Keys.ENTER); 
    source = driver.findElement(By.xpath(OR.getProperty("searchedUserSource"))); 
    destination = driver.findElement(By.xpath(OR.getProperty("userDestination"))); 
    waitUntilElementVisible(30, source); 
    dragAndDrop(source, destination); 
    driver.findElement(By.xpath(OR.getProperty("btnScheduleNow"))).click(); 
} 

public void waitUntilElementVisible(int seconds, WebElement element) { 
    WebDriverWait explicitWait = new WebDriverWait(driver, seconds); 
    explicitWait.until(ExpectedConditions.visibilityOf(element)); 
} 

public void dragAndDrop(WebElement sourceElement, WebElement destinationElement){ 

    try { 
     if (sourceElement.isDisplayed() && destinationElement.isDisplayed()) { 
      Actions action = new Actions(driver); 
      action.dragAndDrop(sourceElement, destinationElement).build().perform(); 
     } 
     else { 
      System.out.println("Element not found to drag and drop"); 
     } 
    } 
    catch (StaleElementReferenceException exc) { 
     exc.printStackTrace(); 
    } 
    catch (NoSuchElementException exc) { 
     exc.printStackTrace(); 
    } 
} 



btnUserGroup = //*[text()='User Groups'] 
btnCreateUG = //*[@id='stage']/div/div[2]/div/div/div[2]/div[3]/a 
textUGName = //input[@id='user_group_name'] 
btnScheduleNow = //*[text()='Schedule Now'] 
userDestination = //*[@class='ConnectedList ConnectedListAdded']/div[2]/ul 
userSearchField = //div[@class='ConnectedListConnectedListSelect']/div[2]/div[1]/ul/li[1]/input 
searchedUserSource = //div[@class='ConnectedList ConnectedListSelect']/div[2]/ul/li/span[5] 

例外:

org.openqa.selenium.StaleElementReferenceException:元素在緩存中沒有發現 - 也許是頁面發生了變化,因爲它是擡頭 命令持續時間或超時:10.28秒

+2

什麼元素是給你的例外呢? – Cathal

+0

在com.suite.regression.RegressionSuiteBase.waitUntilElementVisible(RegressionSuiteBase.java:26) \t在com.suite.regression.UserGroup.addUserBySearch(UserGroup.java:60)我想 「源」 元素 –

+0

在行waitUntilElementVisible(30,資源); addUserBySearch()方法和explicitWait方法。直到(ExpectedConditions.visibilityOf(元件)); waitUntilElementVisible(int seconds,WebElement元素)方法 –

回答

0

StaleReferenceException發生以下兩個原因主要是一個:

  • 該元素已被完全刪除。
  • 該元素不再附加到DOM。

你的問題的一個可能的解決方案是在while循環中包圍dragAndDrop(),並不斷嘗試dragAndDrop(),直到成功。因此,像下面:

boolean draggedAndDropped = false; 
    while(!draggedAndDropped) { 
    try{ 
    source = driver.findElement(By.xpath(OR.getProperty("searchedUserSource"))); 
    destination = driver.findElement(By.xpath(OR.getProperty("userDestination"))); 
    dragAndDrop(source, destination); 
    draggedAndDropped = true; 
    } catch(StaleElementReferenceException e) { 
     System.out.println("StaleElementReferenceException caught, trying again"); 
    } 

}

當然它不是一個好主意主意,讓讓一個循環,無限循環,所以你會想結合了最大努力進入while循環。

-1

如前所述由硒項目的documentation,一個StaleElementReferenceException可以扔掉的原因有兩個:

  1. 的元素已經被全部刪除。
  2. 該元素不再附加到DOM。

因此,我懷疑一個競爭條件在此時發生:

driver.findElement(By.xpath(OR.getProperty("userSearchField"))).sendKeys("testuser2", Keys.ENTER); 
source = driver.findElement(By.xpath(OR.getProperty("searchedUserSource"))); 

你在WebElement你使用的「userSearchField」 Keys.ENTER進入「testuser2」後大概提交搜索查詢 - 問題可能會發生,因爲WebDriver在頁面完全刷新之前正在檢索對「searUserSource」WebElement的引用。

爲了解決這個問題,您可以嘗試使用WebElement .submit()方法提交搜索,因爲它會阻止WebDriver的執行,直到頁面完成更改(即完成顯示搜索結果)。

下面是你如何能夠實現這樣一個例子:

driver.findElement(By.xpath(OR.getProperty("userSearchField"))).sendKeys("testuser2").submit(); 
source = driver.findElement(By.xpath(OR.getProperty("searchedUserSource"))); 
+0

我可以問爲什麼我的答案是downvoted? –