简体   繁体   中英

Select node with text using XPath regardless of whitespace in CasperJS

I'm using CasperJS and want to click a table element that has a certain float value - but there is some preceding whitespace, and I don't know how much of it is there.

For instance:

<td class="narrow value ng-binding"><i class="fa" ng-class="{...}"></i>           1,45</td>

I tried targeting this element by doing:

this.click(x('//*[text()="1,45"]'));

But I get:

Cannot dispatch click event on nonexistent selector: XPath expression: '//*[text()="1,45"]'

Can anybody point out how to ignore the whitespace at the beginning, or why this is not working?

Use normalize-space() to 'ignore' whitespaces at the beginning and end of your string :

//*[normalize-space(text())="1,45"]

or use the following form in case the target text node may not be the first child text node in its parent element :

//*[text()[normalize-space(.)="1,45"]]

From MDN :

The normalize-space function strips leading and trailing white-space from a string, replaces sequences of whitespace characters by a single space, and returns the resulting string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM