normalize-space is a function in XPath which ignores all redundant spaces in a target string HTML element and and returns the resulting string. Here redundant space means:
- Leading Space
- Trailing Space
- Repeated White Space (replaces sequences of whitespace characters by a single space)
Also, it Removes all new lines and tabs present in a string
Table of Contents
- Syntax
- Demo Website
- Using Contains Function
- Normalize-space with Text Function
- Normalize-space with Contains Function
- Normalize-space with attribute
- Normalize-space with attribute and contains
Syntax
normalize-space( [string] )
- Argument: string (optional). The string to be normalized. If omitted, string used will be the same as the context node converted to a string.
- Returns: The normalized string. normalize-space() function takes a string argument and matches the attribute values present on the webpage.
Demo Website
Link for demo website: Auto Pract Normalize Page
<div id = "container">
<div class=" my class 1"> space in beginning and end </div>
<div class=" my class 2"> space in between </div>
<div class = "my class 3">auto <b>pract</b></div>
<div class = "my class 4"><b>programs</b> buzz</div>
</div>
Note: Here, I have tested several scenarios for different function(method) combination to understand the usage of normalize-space function. XPath which is working I have marked it with Pass otherwise fail.
Using Contains Function
Scenario 1: Text with space in beginning and end (PASS)
//div[contains(text(),'space in beginning and end')]
Scenario 2: Text with space in between (FAIL)
//div[contains(text(),'space in between')]
Scenario 3: Text with nested elements with inner text (FAIL)
/div[contains(text(),'auto pract')]
//div[contains(text(),'programs buzz')]
Scenario 4: Attribute with space in beginning and end (PASS)
//div[contains(@class,'my class 1')]
Scenario 5: Attribute with space in between (FAIL)
//div[contains(@class,'my class 2')]
Normalize-space with text() function
Scenario 1: Text with space in beginning and end (PASS)
//div[text()[normalize-space() = 'space in beginning and end']]
//div[normalize-space(text()) = 'space in beginning and end']
Scenario 2: Text with space in between (PASS)
//div[text()[normalize-space() = 'space in between']]
//div[normalize-space(text()) = 'space in between']
Scenario 3: Text with nested elements with inner text (PASS)
//div[text()[normalize-space() = 'auto']]
//div[text()[normalize-space() = 'buzz']]
Scenario 4: Text with nested elements with inner text (FAIL)
//div[text()[normalize-space() = 'auto pract']]
//div[normalize-space(text()) = 'programs buzz']
Normalize-space with contains() function
Scenario 1: Text with space in beginning and end (PASS)
//div[contains(normalize-space(),'space in beginning and')]
Scenario 2: Text with space in between (PASS)
//div[contains(normalize-space(),'space in')]
Scenario 3: Text with nested elements with inner text (PASS)
//div[contains(normalize-space(),'auto pract')]
//div[contains(normalize-space(),'programs buzz')]
Normalize-space with attribute
Scenario 1: attribute with space in beginning and end (PASS)
//div[normalize-space(@class)='my class 1']
Scenario 2: attribute with space in between (PASS)
//div[normalize-space(@class)='my class 2']
Normalize-space with attribute and contains
//div[contains(normalize-space(@class),'my class')]