10

I want to select one element if one out of 2 exist while using this for 2 pages

1st page (price with discount)

<div class="price">
  <span class="originalRetailPrice">$2,990.00</span>
  </div>
</div>
<div class="price">
      <span class="salePrice">$1,794.00</span>
</div>

or 2nd page (only one price)

<div class="price">
  $298.00
</div>

I have used //span[@class="originalRetailPrice"] | (//div[@class="priceBlock"])[1] but I get the price twice

What I want is to select the first price when it's class="originalRetailPrice" or when it's //div[@class="price"]/text()[1]

So finally I want to make the selection to work on both pages

Stéphane Bruckert
  • 21,706
  • 14
  • 92
  • 130
Amr Ali
  • 449
  • 2
  • 11

2 Answers2

9

Use // to get texts at any level inside <div class="price">:

//div[@class="price"][1]//text()

Result:

Text=''
Text='$2,990.00'
Text=''

And filter the empty texts with: text()[normalize-space() and not(ancestor::a | ancestor::script | ancestor::style)]

//div[@class="price"][1]//text()[normalize-space() and not(ancestor::a | ancestor::script | ancestor::style)]

Result 1st page:

Text='$2,990.00'

Result 2nd page:

Text='$298.00'
Community
  • 1
  • 1
Stéphane Bruckert
  • 21,706
  • 14
  • 92
  • 130
  • Perfect , Thanks alot for the fast answer – Amr Ali Jun 01 '15 at 09:53
  • 1
    If you swap div block in 1st xml, result wiil be wrong. It's not the answer – splash58 Jun 01 '15 at 10:38
  • @splash58 this `//div[@class="price"][1]//text()` worked for me , so its an Answer. – Amr Ali Jun 01 '15 at 10:49
  • it is your question you may decide on your mind but the condition `when its class="originalRetailPrice"` is absent in answer – splash58 Jun 01 '15 at 11:31
  • @OP: It may work in particular cases, but it doesn't meet the criteria described in your question, so there are some cases where it will give the wrong answer. – LarsH Jun 01 '15 at 13:19
  • It looks like the `originalRetailPrice` will always be before the `salePrice`. As the OP said: *what i want is to select the **first** price when its class="originalRetailPrice"* – Stéphane Bruckert Jun 01 '15 at 13:33
  • hi said *when its class="originalRetailPrice"*. hi didn't say that no divs before – splash58 Jun 01 '15 at 14:33
4

You can try this way :

//span[@class="originalRetailPrice"] | //div[@class="price" and not(span[@class="originalRetailPrice"])]/text()[1]

The 2nd part (right side of |) select div[@class="price"] element only if it doesn't have child span[@class="originalRetailPrice"].

har07
  • 88,338
  • 12
  • 84
  • 137