2016-06-25 37 views
-1

我是selenium ide的新手。嘗試使用具有相同值的類獲取第二個html標記。如何識別使用類和HTML標籤位置的Xpaths

<div class="test"> 
<div> 
<small class="jo-date-time text-muted">Ordered on Jun 23, 2016 </small> 
</div> 
</div> 
<div class="test"> 
<div> 
<small class="jo-date-time text-muted">Ordered on Jun 22, 2016 </small> 
</div> 
</div> 

例如, class=jo-date-time[2] and //small[@class='jo-date-time'][2]

但是,它不起作用。它仍然需要父html標籤。這是真的嗎?

這一個適合我。 - >//div[@class='test'][2]/div/small

回答

0

這些xpaths不工作,因爲它們不正確。

如果您注意到class屬性,則會有一個空格,然後是您省略的文本。這是它不工作的原因。

變化來自: //small[@class='jo-date-time'][2]

要:(//small[@class='jo-date-time text-muted'])[2]

enter image description here

,我個人不使用索引,如果有辦法避免可能會有所不同。

這裏是另一種方式避免使用索引:
//small[@class='jo-date-time text-muted' and . = 'Ordered on Jun 22, 2016 ']

+0

我試過使用(//小[@ class ='jo-date-time text-muted'])[2]雖然它仍然不起作用。原因我是usi ng索引是因爲數據是動態的並且可能會改變。我只是想檢查它們是否存在。順便說一句,它應該真的用()括起來嗎? –

+0

@LovieT,你想提供更多的細節?你如何使用它?我提供的是一個測試結果 – Rao

0

如果該元素是由類名喬·日期時間
然後嘗試位於包含XPath中
功能

//small[contains(@class,'jo-date-time')][2] 
+0

這不起作用。 – Rao