Xpath使用sibblings或在两个不同的Cell

时间:2017-06-16 14:24:57

标签: html xpath

直截了当地说我想在td中找到TestCoupon10%然后打开兄弟td然后找到// [包含(@id," cmdOpen")]我确实尝试过兄弟姐妹和同伴但很可能我没做它是对的,因为

//跨度[./文本()=" TestCoupon10%"] /以下同胞:A [含有(@id," cmdOpen&#34)] 导致无效的xpath。 HTML结构看起来像是

<tr>
 <td>
  <span id="oCouponGrid_ctl03_lblCode">TestCoupon10%</span>
 </td>
 <td>...</td>
 <td>...</td>
 <td valign="middle" align=""right">
  <a id="oCouponGrid_ctl03_cmdOpen">
 </td>
</tr>

我需要找到cmdOpen和测试优惠券有没有人知道如何?

1 个答案:

答案 0 :(得分:1)

轴用双冒号分隔,而不是单冒号(用于命名空间前缀)。你想这样说:

//span[./text()="TestCoupon10%"]/following-sibling::a[contains(@id,"cmdOpen")] 

- <a>不是<span>的后续兄弟。你需要做一些导航:

//span[./text()="TestCoupon10%"]/parent::td/following-sibling::td/a[contains(@id,"cmdOpen")]

或者,只是避免下降到树中,您不必首先“爬上去”。

//td[span = "TestCoupon10%"]/following-sibling::td/a[contains(@id,"cmdOpen")]