Join us on : <

Normalize space Xpath

Eliminates redundant spaces from the supplied string.

normalize-space function is a part of string function in XPath. normalize-space function takes a string argument and matches the string present in the document/webpage.

normalize-space function ignores the leading, trailing, and repeating white spaces, that means after apply the normalize-space the text become normalized with no line breaks, and give a proper sentence.



									str = "   cher      cher     tech     "
									normalize-space(string) // outputs "cher cher tech"

Specifically, Normalize space performs three steps:

  • All leading whitespace is removed.
  • All trailing whitespace is removed.
  • Within the string, any sequence of whitespace characters is replaced with a single space.
  • Removes all new lines and tabs present in string

Video Tutorial for Normalize Space in xpath

Subscribe to my youtube channel :

how to use normalize-space in xpath

We can create a xpath using the normalize space, these kind of xpaths will be useful when we want to match an element but which has leading/trailing spaces or more spaces between words.

xpath normalize-space text example

									html code
									<label>    who cares         about spaces   </label>

									//label[normalize-space(text())='who cares about spaces']

normalize space xpath in selenium

Not only developers uses the xpath but testers also do use xpah for automating the application. normalize space plays importatcnt role in selenium automation where we have to find the elements on the webpage and perform some action on it with webdriver.

									driver.findElement(By.xpath("//label[normalize-space(text())='who cares about spaces']")).click()

normalize space in xpath example

normalize-space('query')	-->	query
normalize-space(' query ')	-->	query
normalize-space('xml query')	-->	xml query
normalize-space('xml       query')	-->	xml query
					query')	-->	xml query
normalize-space('')	-->	zero-length string
normalize-space('   ')	-->	zero-length string
normalize-space(())	-->	zero-length string
normalize-space(      query  )	-->	query

xpath normalize-space attribute

So far we have seen like how to use the normalize space with text, normalize-space can also work with attribute which has speces and new lines

html code
<label id='has    random   spaces       '>text </label>

//label[normalize-space(@id)='has random spaces']

xpath normalize space contains

We can also match the partial match using the normalize-space with contains() function present in xpath. Rewriting the above example using contains with normalize-space

									html code
									<label id='has    random   spaces       '>text </label>

									//label[contains(normalize-space(@id),'has random')]

xpath normalize-space examples

The xpath does not depend on the programming languages like java, C#, python or any other but it always depends on teh HTML document or XML doocument.

About Author

Article is written by Pavan (a) KarthiQ. Well, I am serving notice period in an MNC, Bangalore. I thought to enrich every person knowledge a little, I always have a feeling, when we teach something, we will learn more than what you know. Knowledge is the only thing that doubles when you spend it.

I have also created the reporter for Protractor Jasmine. Use for your projects without any hesitation

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions