I am trying to use this xpath to find the number of elements that have a value between X and Y. The problem I am facing is since there is &nbsp in the HTML, and the code doesn't detect the text value.


    //div[@class="prix"]/div[2]/div[2]/span[1][number(translate(text(), ",", ".")) > 4 and number(translate(text(), ",", ".")) < 43]


    <div class="d-flex-inline productPrice">

I tried with replace(text()," ","") and replace(text(),"\&nbsp;\","") and other combinations but nothing worked.

  • I thought number stripped whitespace?
    – ernie
    Feb 4 at 0:40

3 Answers 3


It's easier to use xpath simply to locate the element and treat fetched string using your programming language.

For example, in Java, you can use a regex to replace anything that is not a number or dot with an empty character, thus keeping only the number representation.

String onlyNumbers = str.replaceAll("^\d+(\.\d+)*$", "");
  • yeah normaly but for less code i try to get the size of that xpath so if the size is equal to what i have i want need to creat a "for" function to test every article that shows
    – Yan
    Feb 3 at 14:33

If your HTML Code is this,

    <div class="d-flex-inline productPrice">

Then you can get only the number and ignore the space or any other character from the text you get from getText() using this,

driver.findElement(By.xpath("//div[@class='d-flex-inline productPrice']/span[1]")).getText().replaceAll("[^\\p{ASCII}]|&nbsp;| ", "");

Here is my working code,

try {
            WebDriver driver;
            System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "//chromedriver");
            driver = new ChromeDriver();
            driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
            System.out.println(driver.findElement(By.xpath("//div[@class='d-flex-inline productPrice']/span[1]")).getText().replaceAll("[^\\p{ASCII}]|&nbsp;| ", ""));
        } catch (Exception e) {
            // TODO Auto-generated catch block

This a common scenario faced by testing companies who provide automation testing services.

To remove spaces from the text we can have a regular expression check and replace HTML  

String myStr = "4,42&nbsp";
myStr = myStr.replaceAll("[\\s\\u00A0]+$", "") ;

It removes non breaking spaces in the input string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.