2014-05-09 64 views
3

我在網頁中有以下元素。我想獲取ID爲「pmt-id-234」的元素,該元素有一個類名爲type2的後代。Xpath:找到一個元素,其後裔已獲得一定的屬性值

<div id="cards"> 
    <div id="pmt-id-123" class="payments"> 
     <div> 
      <div class="type1">Text1</div> 
     <div> 
    </div> 

    <div id="pmt-id-234" class="payments"> 
     <div> 
      <div class="type2">Text1</div> 
     <div> 
    </div> 
</div> 

注:

  1. 我不知道 「PMT-ID- 」 的高亮部分,帶有ID因此直接查詢是不可能的。
  2. class =「typeX」的div可以向下嵌套多個級別。

什麼是嘗試?下面給了我兩個div元素。現在

'//*[@id="cards"]//*[starts-with(@id,"pmt-id-")]' 

,如何獲取其中有帶class =「2型」

以下din't產生任何結果的後裔股利股利。

'//*[@id="cards"]//*[starts-with(@id,"pmt-id-")//*[contains(@class, "type2")]]' 
'//*[@id="cards"]//*[starts-with(@id,"pmt-id-")][contains(@class, "type2")]' 

請讓我知道如何做到這一點?

回答

3

如果在那裏只有divs,我會針對div而不是*進行測試。

這個XPath將與cards的ID具有與pmt-id-開頭的ID選擇div一個下,也有type2類的子div

'//div[@id="cards"]//div[starts-with(@id,"pmt-id-") and .//div[contains(@class, "type2"]]' 

請注意,您可能需要採取額外的注意與@class匹配,以避免匹配type22abctype2(如果可能的話)。

+0

感謝@kjhughes,這工作!是的,他們不是所有的div。 – ragesh

相關問題