简体   繁体   中英

Something weird happening with the casperjs/phantom js code (casper.exists() function)

Sample Code:

function myfunc(){
    ctxt = "this is the" + variable1 + "value";
    if (casper.exists(x('//p[@class="classname" and contains(text(), ctxt)]{
            //code1;
    }
}

When myfunc is called, the if block gets executed every time no matter whether ctext exists or not. But if I hardcode the text instead of ctxt for example "abcde" , then it's execution doesn't occur.

This is what I mean:

contains(text(), "abcde")

There is probably a problem with the space around the position where variable1 is inserted to. You could try to define ctxt like this:

ctxt = "this is the " + variable1 + " value";

The exists call should look like this:

casper.exists(x('//p[@class="classname" and contains(text(), "'+ctxt+'")]'))

You need to fix the closing brackets: ]{ should be ]'))){ .

The other thing is that you forgot to use the variable. Instead you just inserted the variable name ( ctxt ) into the XPath expression and not the variable value "'+ctxt+'" .

If this still does not work, you can try to normalize the white space of the text:

casper.exists(x('//p[@class="classname" and contains(normalize-space(text()), "'+ctxt+'")]'))

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