0

I started learning Xpath somedays ago, and it is not clear for me that this technology is capable of doing that:

---> I have a table which has a lot of row, the row always contains an img with a clickable href on it

<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>  

---> I use now this to get the href:

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

where i is the position in the html (1- first, 2- second....)

---> i want only to select this href, when has a sibling with the specific value (SPECIFIC_USER):

<b>Elemezte:   SPECIFIC_USER</b>

OR HAS A Sibling with the given child (SPECIFIC USER):

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

So i do not know is it possible with XPATH or only with JavaScript, I copied the html source one of the given rows--->

 <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>
czupe
  • 4,740
  • 7
  • 34
  • 52
  • Good question! :) currently fighting with it! – hek2mgl May 05 '13 at 10:51
  • me too:) do you know any good online xpath tester/visualizer? (no good source/material found by me so far about this topic : ( http://www.huttar.net/dimitre/XPV/TopXML-XPV.html No more working : ( – czupe May 05 '13 at 10:56
  • I prefer to use a little PHP script. But there a lot of. Btw, in which programming language do you want to implement it? (some support xpath1.0, some even 2.0) – hek2mgl May 05 '13 at 11:03
  • I use selenium which is a testing tool, to automatize test cases for web pages. And if i am right it is use Xpath 1.0 due to this: http://stackoverflow.com/questions/1936301/can-i-use-xpath-2-0-with-firefox-and-selenium... Now i use Selinium ide for this, it looked an easy task, but now it is not anymore : ) Update: Ok, sorry : ) – czupe May 05 '13 at 11:06
  • Yep, I know selenium. Good to know that selenium just supports XPath1.0 (like my PHP test script ;) – hek2mgl May 05 '13 at 11:07
  • Btw, with this query you can access the text: `'//a[@href="thread.php?..."]/following-sibling::b/text()'` Have answered this but deleted after reading your question again ;) ... If we will not find a pure XPath solution, you may use this in a script to determine if the text matches, if so you could issue a second query for the @href. As you don't know the link exactly you would need to use `starts-with()` function – hek2mgl May 05 '13 at 11:10
  • so you think there is no pure xpath solution for this task? Anyway is there any html xpath tester? (i found a lot of xml tester, but not works fine with html) – czupe May 05 '13 at 11:13
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/29434/discussion-between-hek2mgl-and-czupe) – hek2mgl May 05 '13 at 11:13

1 Answers1

1

You can use the following query. It uses the following-sibling axis to get the following <b> element text() value and compares it to Elemezte: SPECIFIC_USER. If it matches, the href attribute of the <a> node will be selected. It just selects the second of those elements found:

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

What will give you:

"thread.php?judgelock=1&threadid=134389"
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • not bad!:) No chance to get only the second result? "thread.php?judgelock=1&threadid=134389" because i want to click on that:D – czupe May 05 '13 at 12:40
  • just add the `[2]`.. Check my upate – hek2mgl May 05 '13 at 16:39
  • Ok Dude, thank you very much again... Unfortunately i cannot select the href with the given XPATH but i will try... (The original source code is so mutch more than this part...) – czupe May 06 '13 at 09:03