Javascript/Typescript How to remove/replace   from textContent()

Andy

Admin
Testing Bytes
You may have a situation where you are getting a value from a table cell for example that renders like:
Code:
Expand Collapse Copy
Hello World

But in the DOM is:
HTML:
Expand Collapse Copy
<td>Hello&nbsp;World</td>

If you then were checking the textContent of such a cell to compare if it is equal to something:

Typescript:
Expand Collapse Copy
if (locator.textContent() === 'Hello World') {
    // do something
}

That will not work, because the string will have the &nbsp;'s corresponding character code of 160, so you need to santize the value before comparing:

Typescript:
Expand Collapse Copy
let cell = (await headerCells.nth(i).textContent());
           
cell = cell.trim().toLowerCase().replace(/\u00a0/g, ' ')

Now the string comparison will work.
 
Last edited:
Back
Top