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.
Syntax
normalize-space(string)
str = " cher cher tech "
normalize-space(string) // outputs "cher cher tech"
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>
xpath
//label[normalize-space(text())='who cares about spaces']
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('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
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>
xpath
//label[normalize-space(@id)='has random spaces']
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>
xpath
//label[contains(normalize-space(@id),'has random')]
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.
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