2014-10-08 125 views
0

我需要從代碼訪問folderId的:StaleElementReferenceException:陳舊的元素參考:

{ 
"folderType": 3, 
      "createdDate": "02.09.2014 11:57:28.UTC", 
      "modifiedDate": "02.09.2014 11:57:28.UTC", 
      "folderName": "Enterprise", 
      "folderId": "baebc0fb-38cb-4300-9bd1-9b98fa622e4a", 
      "widgetType": null, 
      "enterpriseId": "366fc1f1-670d-41bc-905e-bc80accd2537", 
      "parentFolderId": "", 
} 
{ 
"folderType": 3, 
      "createdDate": "02.09.2014 11:57:28.UTC", 
      "modifiedDate": "02.09.2014 11:57:28.UTC", 
      "folderName": "Enter", 
      "folderId": "6671ca49-9637-488e-9a0e-f7dbf415a542", 
      "widgetType": null, 
      "enterpriseId": "366fc1f1-670d-41bc-905e-bc80accd2537", 
      "parentFolderId": "", 
} 

我需要訪問「企業」和「輸入」文件夾的folderId。

這是我的代碼:「[。= //跨度 '\ 」企業\「']

字符串S1 = driver.findElement(By.xpath(/祖先::前/以下同胞::預[1] /跨度[2]「))的getText();

String s2=driver.findElement(By.xpath("//span[. = '\"Enter\"']/ancestor::pre/following-sibling::pre[1]/span[2]")).getText(); 

我想打印沒有cotes的字符串s1和s2的值。 我可以訪問'企業'folderId但無法訪問'輸入'folderId。 由於'輸入'folderId是新創建的文件夾。

對於柯特斯之間各個領域,以下是GENERAL FORAMT HTML代碼:

<pre>   <span class="cm-string">"folderName"</span>: <span class="cm-string">"Enter"</span>,</pre> 
<pre>   <span class="cm-string">"folderId"</span>: <span class="cm-string">"6671ca49-9637-488e-9a0e-f7dbf415a542"</span>,</pre> 

我可以使用Chrome開發者工具這樣的選擇輸入folderId:

$x("//span[. = '\"Enter\"']/ancestor::pre/following-sibling::pre[1]/span[2]") 
[<span class=​"cm-string">​"6671ca49-9637-488e-9a0e-f7dbf415a542"​</span>​] 

實際輸出在eclipse中: 線程「main」中的異常org.openqa.selenium.StaleElementReferenceException:陳舊元素引用:元素未附加到頁面文檔 (會話信息:chrome = 37.0.2062.124)

+0

在什麼瀏覽器,你測試它? – Braj 2014-10-08 19:03:49

+0

Chrome Browser.Its鉻擴展'郵遞員休息客戶'我試圖自動化。 – user3174553 2014-10-08 19:05:31

回答

2

StaleElementReferenceException是由於方法存在元素被訪問的不可用性。下面的示例代碼

使用等待元素的可見性:

private static WebElement findElement(WebDriver driver, By locator) { 
    Wait<WebDriver> wait = new FluentWait<WebDriver>(driver) 
      .withTimeout(90, TimeUnit.SECONDS) 
      .pollingEvery(5, TimeUnit.SECONDS) 
      .ignoring(NoSuchElementException.class, StaleElementReferenceException.class); 
    return wait.until(ExpectedConditions.visibilityOfElementLocated(locator)); 
} 

Read more...

+0

所以我在這裏有一個疑問,只要我可以找到使用開發人員工具在鉻中的元素,如果它仍然不能在Eclipse中工作。這是什麼原因?我多次遇到這樣的問題。 – user3174553 2014-10-09 08:19:54

+0

'FluentWait'將解決首先等待元素可見性的問題。對不起,我對此沒有深入的瞭解。 – Braj 2014-10-09 08:32:06