最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use ignorecase when using contains(text(), "string") in XPath expressions in CasperJS? - S

programmeradmin8浏览0评论
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."))]

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论