XPath normalize-space example in Selenium WebDriver



In this example we will discuss about the usage of normalize-space in Xpath.

We already aware of the Functions like text(),contains(),starts-with(),last() and position() in Xpath. Refer to this link to know details about these functions. Xpath Functions.

Generally, normalize-space(String s) is a method in Xpath useful to remove any leading or trailing spaces in a String. This is also works like a trim() function in java.lang.String class. We will see different examples

In the above example the table data has spaces for Thomas Cook. If we use Xpath Text Function it doesnt provide any value as an output. Please check the screenshot below:

Usage of Xpath text() Function

Usage of Xpath text() Function

Usage of normalize-space(text()) method in Xpath.Please check the screenshot below:

xpath-normalize-space-example

xpath-normalize-space-example

Selenium WebDriver Example to fetch the price for a particular Stock:

Therefor the extra spaces in the table data is ignored and identified the element and fetches the stock price.

Output:

Stock Price::: 244.60

Conclusion:

In the example we have learnt how to use the normalize-space in Xpath.

Leave a Reply

Your email address will not be published. Required fields are marked *