2014-09-26 24 views
2

這是我的HTML代碼:切換到另一種使用XPath

<pre> 
<span class="cm-string">"userId"</span> 
":" 
<span class="cm-string">"abc"</span> 
"," 
</pre> 

我需要從「用戶id」標籤捕獲文本「ABC」作爲值ABC保持瞬間改變。

我試着用下面的代碼,它失敗了。

driver.findElement(By.xpath("//span[contains(text(),'userId')]/span")).getText(); 
+0

你的問題就沒有意義了。沒有'userId'標籤。 「userId」本身就是「span」元素的文本內容。另外,你的java代碼沒有意義,因爲你選擇了一個包含某個字符串的span,然後......返回這個字符串? – 2014-09-26 13:40:27

回答

2

abcspan是以下同級含UserIdspan,使用它:

//span[contains(., 'userId')]/following-sibling::span 
+0

另一個疑問是,有很多子字符串的名稱爲「userId」,我如何找到確切的字符串「userId」?下面的代碼給出錯誤,因爲cotes //包含(包含(。,''userId'')]/following-sibling :: span – user3174553 2014-09-26 14:27:06

+0

@ user3174553當然,只是轉義引號而不使用'contains()':'/ /跨度[。 ='\「userId \」']/following-sibling :: span' – alecxe 2014-09-26 17:03:00

+0

 "userId" ":" "abc" "," 
 "password" ":" "xyz" "," 
在上面的代碼中,我如何從abc訪問xyz。如果你用最後一段代碼幫助我,我會非常感激。 – user3174553 2014-09-26 19:16:51

相關問題