1

I'm trying to write an evaluate function in CasperJS, but I'm running into an issue when I call a click event. Here's a quick code block to illustrate the problem:

returnVal = casper.evaluate(() ->
    document.querySelector('.class').click()
    return true

returnVal is always null in the above, even when the class exists.

In my actual code I have a good deal of code to run, and I want to do multiple click events throughout.

Is it the design of CasperJS to immediately return when a click() is called? Or am I missing something really simple?

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Connor Black
  • 6,921
  • 12
  • 39
  • 70

1 Answers1

1

When you register to some error event like "page.error":

casper.on("page.error", function(msg, trace) {
    this.echo("Error: " + msg);
});

then you will see that click() is not a function that can be called. PhantomJS 1.x only supports click() on <input> and <button> elements and even that is not consistent. Use CasperJS' own click function if you want consistent behavior:

casper.then(function(){
    this.click(".class");
});

The result of the evaluate() call is always null (undefined), because the return statement is never executed. An uncaught error is thrown in the line before that.

This is written in JavaScript, but applies in exactly the same way to CoffeeScript.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
  • Thanks. Is there absolutely no way to do a normal JavaScript click event in a evaluate function? The website I'm dealing with uses a virtual DOM, so `this.click(".class")` unfortunately doesn't work – Connor Black Nov 18 '15 at 23:14
  • You can add a custom synthetic click function inside of the page context in [this way](http://stackoverflow.com/a/15948355/1816580), but I doubt this will work better. I guess you need to figure out what the real DOM nodes behind the virtual nodes are. – Artjom B. Nov 18 '15 at 23:18