admin管理员组

文章数量:1394134

if (casper.exists(x('//p[@class="classname" and (contains(text(), "this is my string."))]'))){
    //code
}

I want to be able to match "this is my string." as well as "thiS is My striNg.". I couldn't find any functions to do this. It is okay to change the text on the screen to lowercase or uppercase and then match but it shouldn't change all the text rather just the string that I want to search for. But I couldn't find how to do this.

if (casper.exists(x('//p[@class="classname" and (contains(text(), "this is my string."))]'))){
    //code
}

I want to be able to match "this is my string." as well as "thiS is My striNg.". I couldn't find any functions to do this. It is okay to change the text on the screen to lowercase or uppercase and then match but it shouldn't change all the text rather just the string that I want to search for. But I couldn't find how to do this.

Share Improve this question edited Jul 16, 2014 at 10:21 Artjom B. 62k26 gold badges135 silver badges230 bronze badges asked Jul 7, 2014 at 16:00 aste123aste123 1,2424 gold badges21 silver badges43 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

It seems that Casper JS only supports XPath 1.0. In that case you can't use the lower-case() function, but you can use translate() to replace a collection of characters with their lower-case representation. You need to include all the characters that can have upper and lowercase representations (including accented characters, for example), in order.

For example: the expression:

translate('street', 'tse', 'sto')

replaces each t for an s, each s for a t and each e for an o. It results in:

tsroos

So you can use

translate(text(),'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')

to translate all upper-case ASCII characters in your string to lower-case. Applying this you your example, you can use:

//p[@class="classname" 
  and (contains(translate(text(),"ABCDEFGHIJKLMNOPQRSTUVWXYZ",
                                 "abcdefghijklmnopqrstuvwxyz"), 
                                 "this is my string."))]

本文标签: