user3748741
user3748741

Reputation: 23

Using XPath to find a dynamic table value

In this table with dynamically changing prices, I wish to always select the link belonging to the first price over $400,00 in XPath 1.0. The correct solution might not always be in the second row, so tr[2]/td[1] will not always be the correct result.

<table>
   <tr>
      <td><a href="/somesite/page.htm?id=1">1</a></td>
      <td>-$200,00</td>
   </tr>
   <tr>
      <td><a href="/somesite/page.htm?id=2">2</a></td>
      <td>$500,00</td>
   </tr>
   <tr>
      <td><a href="/somesite/page.htm?id=3">3</a></td>
      <td>$100,00</td>
   </tr>
</table>

My (non-working) XPath that comes closest so far is:

//tr/td[2][starts-with(.,'$')]/(number(substring-after(.,'$')))>400.00/preceding-sibling::td

Here is the problem:

Any help would be really appreciated (I'm just a simple tester getting dragged into the magical world of test automation) :)

Upvotes: 2

Views: 585

Answers (1)

Tomalak
Tomalak

Reputation: 338406

I wish to always select the link belonging to the first price over $400,00 in XPath 1.0.

That would be

//td[number(translate(normalize-space(), ',$', '.')) > 400]/..//a

Note that I translate , to . and $ to nothing**, so if your numbers are formatted any further (digit grouping, for example) this might not be 100% correct yet. Know your data, make the appropriate changes.

For the fun of it (and to demonstrate XPath's flexibility), here's a different path with the same result:

//a[../..//td[number(translate(normalize-space(), ',$', '.')) > 400]]

** translate() replaces one list of characters with another:

  • translate('abc', 'ac', 'AC') returns 'AbC'
  • translate('abc', 'ac', 'A') returns 'Ab'.

Upvotes: 1

Related Questions