0

There is for example such piece of code:

<tr>
    <td width="270" class="news">
        <a href="/lists/test_acc/1/" class="all">Ukraine</a>
        <span>17.06.15<span>
    </td>
</tr>

<tr>
    <td width="270" class="news">
        <a href="/lists/test_acc/1/" class="all">France</a>
        <span>17.06.15<span>
    </td>
</tr>

<tr>
    <td width="270" class="news">
        <a href="/lists/test_acc/1/" class="all">USA</a>
        <span>17.06.15<span>
    </td>
</tr>

<tr>
    <td width="270" class="news">
        <a href="/lists/test_acc/1/" class="all">France</a>
        <span>10.10.15<span>
    </td>
</tr>

<tr>
    <td width="270" class="news">
        <a href="/lists/test_acc/1/" class="all">Germany</a>
        <span>17.06.15<span>
    </td>
</tr>

<tr>
    <td width="270" class="news">
        <a href="/lists/test_acc/1/" class="all">France</a>
        <span>23.12.15<span>
    </td>
</tr>

In dev panel in Chrome this xpath //tr/td/a[text()="France"] shows me 3 results. I cant find the way to get the value I need.

For example, how to get the date of 2nd result (10.10.15)? I tried different ways with position, something like //tr/td/a[text()="France"][position=2]/span/text() and //tr/td/a[position(text()="France")][2]/span/text()etc, but no success. Maybe "position" is not what I need?

EDITED: Positions of "France" in code differ depends on a page. In the example above right now positions are 2,4,6, but on the other page it can be 3,10,15,25,27. How to get the 2nd result and the last one, without taking into account the position where 'France' actually located?

TitanFighter
  • 4,582
  • 3
  • 45
  • 73

2 Answers2

1

This is one possible way :

(//tr/td[a='France'])[2]/span/text()

The parentheses before position index is required because ([]) has a higher precedence (priority) than (// and /) *. So this expression //tr/td[a='France'][2]/span/text(), for example, will look for the 2nd td within a single tr parent instead, which doesn't exist in the HTML sample you posted.

*: for further explanation on this matter: How to select specified node within Xpath node sets by index with Selenium?

Community
  • 1
  • 1
har07
  • 88,338
  • 12
  • 84
  • 137
  • I would also suggest selecting the 2nd `tr` and then selecting the span underneath: something like `//tr[td/a='France'][position()=2 or position()=last()]//span/text()` (2nd and last being mentioned in OP's edit) – paul trmbrth Nov 18 '15 at 12:09
0

Your 'xml' was invalid so first I corrected it (I know it isn't valid HTML but good enough to test):

<?xml version="1.0" encoding="utf-8"?>
<form>
 <tr>
   <td width="270" class="news">
     <a href="/lists/test_acc/1/" class="all">France</a>
     <span>17.06.15</span>
   </td>
 </tr>
 <tr>
   <td width="270" class="news">
     <a href="/lists/test_acc/1/" class="all">France</a>
     <span>10.10.15</span>
   </td>
 </tr>
 <tr>
   <td width="270" class="news">
     <a href="/lists/test_acc/1/" class="all">France</a>
     <span>23.12.15</span>
   </td>
 </tr>
</form>

Then, this worked:

//tr[position()=2]/td/a[text()="France"]/../span/text()
Steve Wellens
  • 20,506
  • 2
  • 28
  • 69