当兄弟具有特定值时,选择链接的Xpath

时间:2013-05-05 10:19:03

标签: html xpath

我有一天开始学习Xpath,而且我不清楚这项技术是否能够做到这一点:

--->我有一个有很多行的表,行总是包含一个带有可点击的href的img

<a href="thread.php?judgelock=1&threadid=134389">
  <img src="http://www.xxxxx.com/icons_wbb/icon12.gif" alt="Judge" title="Open to Judge" border=0>
</a>  

---&GT;我现在用它来获取href:

(//img[@alt='Open'])[${i}]

其中i是html中的位置(1-第一,第二......)

---&GT;我想只选择这个href,当有一个具有特定值的兄弟(SPECIFIC_USER)时:

<b>Elemezte:   SPECIFIC_USER</b>

或与给定的孩子(特定用户)有兄弟姐妹:

<a href="profile.php?username=SPECIFIC_USER">SPECIFIC_USER</a></b>

所以我不知道是用XPATH还是只用JavaScript,我复制了html源代码行之一---&gt;

 <tr align="center">
  <td class="tableb"></td>
  <td class="tablea" style="width:80%" align="left"><span class="normalfont">
  <a href="thread.php?threadid=134389">[2013.04.28]</a>
  <a href="thread.php?judgelock=1&threadid=134389">
  <img src="http://www.xxxxx.com/icons_wbb/icon12.gif" alt="Open to Judge" title="Open to Judge" border=0>
  </a>  
<b>Elemezte:   SPECIFIC_USER</b>
  </span><span class="smallfont">   
  </span></td>
  <td class="tableb">
    <span class="normalfont">
            <a href="javascript:who(134389)">7</a>
    </span>
  </td>
  <td class="tablea" style="width:20%"><span class="normalfont">
<a href="profile.php?username=SPECIFIC_USER">Clickbox</a>
</span></td>
  <td class="tableb"><span class="normalfont">58</span></td>
  <td class="tablea" nowrap="nowrap">&nbsp;</td>
  <td class="tableb" align="left"><table cellpadding="0" cellspacing="0" border="0" style="width:100%">
   <tr align="right" class="tableb_fc">
    <td align="right" nowrap="nowrap"><span class="smallfont"><b>Ma</b>, <span class="time">11:15</span><br />
    írta: 
<b>
<a href="profile.php?username=SPECIFIC_USER">SPECIFIC_USER</a></b>
</span></td>
   </tr>
  </table></td>
 </tr>

1 个答案:

答案 0 :(得分:1)

您可以使用以下查询。它使用following-sibling轴获取以下<b>元素text()值,并将其与Elemezte: SPECIFIC_USER进行比较。如果匹配,则将选择href节点的<a>属性。它只选择了第二个找到的元素:

'//a[following-sibling::b/text() = "Elemezte:   SPECIFIC_USER"][2]/@href'

什么会给你:

"thread.php?judgelock=1&threadid=134389"