Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Xpath always give true

I have this case where i have a filter this filter is stuck between 88 and 89.
The probleme is the values i can have are 88,36 and 88,95.
My actualy case :

<div class="d-flex-inline">
<span>88,97</span>
<span>€/HT</span>
</div>

When i tried to give him this Xpath it always return true either i give it a correct condition or not and always find the element.

/div[@class="prix"]/div[2]/div[2]/span[1][boolean(number(text()) > 80) = false] Returns true
/div[@class="prix"]/div[2]/div[2]/span[1][boolean(number(text()) < 80) = false] Returns true

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

the xpath i want is one where i can give the initial filter values 88 and 89 i was planning to add the other condition if the bool worked but it didn’t can you guys please assist me with this one.

FYI : When i tried

//div[@class="prix"]/div[2]/div[2]/span[1][number(text()) = 88,97]

it didn’t work and didnt find the element

>Solution :

The XPath definition of a Number only allows . as the decimal separator, not , The number() function uses that definition, and returns NaN ("not a number") for any string not in the expected format.

Since NaN can’t be defined as either less than or greater than any specific value, the condition always returns false. (Your title says it always returns true, but that’s because you’re inverting it with = false).

You can use the translate() function to replace the , with a . and then the number should parse correctly:

number(translate(text(), ",", "."))

In context:

/div[@class="prix"]/div[2]/div[2]/span[1][boolean(number(translate(text(), ",", ".")) > 80) = false]

Incidentally, boolean(foo) = false can more simply be written with the not function as not(boolean(foo)), or just not(foo):

/div[@class="prix"]/div[2]/div[2]/span[1][not(number(translate(text(), ",", ".")) > 80)]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading