我有一棵樹是這樣的:
<table>
<tbody>
<tr>
<td>
<span>MySpan1</span>
</td>
<tr>
<tr>
<td>
</td>
<td>
<div>
<a title="my_title1"></a>
</div>
<td>
</tr>
<tr>
<td>
<span>MySpan2</span>
</td>
<tr>
<tr>
<td>
</td>
<td>
<div>
<a title="my_title2"></a>
</div>
<td>
</tr>
</tbody>
</table>
我需要選擇<a title="my_title2"></a>知道它的tr父級必須有一個兄弟tr/td/span[contains(text(),'MySpan2')]
如果使用 css 選擇器無法實作,那么可以使用 xpath 實作嗎?
uj5u.com熱心網友回復:
請使用此 xpath
//span[contains(text(),'MySpan2')]/ancestor::tr/following-sibling::tr[2]/descendant::a[@title='my_title2']
我們的目標是MySpan2跨度,然后轉到ancestor哪個,tr 然后我們必須轉到它的第二個following-sibling,然后定位一個帶有標題的標簽my_title2
你不能css selector為此使用,xpath只是選擇。
我們正在向上穿越,HTMLDOM所以css必須被排除在外。
請參考xpath 軸
更新 :
與tr兄弟姐妹編號無關:
//span[contains(text(),'MySpan2')]/ancestor::tr/following-sibling::tr/descendant::a[@title='my_title2']
uj5u.com熱心網友回復:
這對我有用:
//span[contains(text(),'MySpan2')]/ancestor::tr/following-sibling::tr/td/div/a[@title='my_title2']
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/313856.html
上一篇:使用硒更改表中的值
