Normalize space XPath

Eliminates redundant spaces from the supplied string.

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

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

Syntax


									normalize-space(string)

									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 a string

Video Tutorial for Normalize Space in xpath

Subscribe to my youtube channel :


how to use normalize-space in xpath

We can create an xpath using the normalize space, these kinds 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>

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

normalize space xpath in selenium

Not only developers use the xpath but testers also do use xpath for automating the application. normalize space plays an important 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
normalize-space('xml
					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 an attribute which has spaces and new lines


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

xpath
//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 the xpath. Rewriting the above example using contains with normalize-space


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

									xpath
									//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 the HTML document or XML document.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions