Unable to click a button element using CasperJS that has an onclick event attached to it

1,024 views
Skip to first unread message

Gavin

unread,
Aug 28, 2013, 5:16:12 AM8/28/13
to casp...@googlegroups.com
I am trying to do what looks like a very simple click event using CasperJS on the following URL:

Using the following selector ('div.add-to-cart-btn > button.btn-primary') I am able to use jQuery to 'find' the button that I wish to click, when I run the following code it prints the line to the CMD window so it is definitely able to find the button via the selector.

if (this.exists('div.add-to-cart-btn > button.btn-primary')) {
      this.echo('FOUND div.add-to-cart-btn > button.btn-primary', 'INFO');
}

However when I use the following code it doesn't seem to be able to 'click' this button for some reason - I have noticed this button has an onclick event attached to it, do I need to anything different to run the onclick event (I presumed the following code would be sufficient) - using the capture() I can see that the button hasn't been clicked.

this.evaluate(function() {
    $('div.add-to-cart-btn > button.btn-primary').click();
});  

Here is the jsfiddle with the entire code

I am using CasperJS 1.1.0-Dev & PhantomJS 1.9 (on WAMP server)
Note - I have asked this previously but condensed my question this time to make it more concise.

Diego Armando

unread,
Jan 11, 2018, 8:46:05 AM1/11/18
to CasperJS
I'm with the same issue. Probably some js are not being loaded. use casper.on("page.error", function() { }) to see what is happening.

Ken Soh

unread,
Jan 20, 2018, 7:06:31 AM1/20/18
to CasperJS
In normal situations, using casper.click() should work. It could be browser incompatibility with the webpage. CasperJS underlying browser is PhantomJS, and it is diverging from current mainstream webapps which are developed to work on Chrome browser, or Safari.

http://docs.casperjs.org/en/latest/modules/casper.html#click
Reply all
Reply to author
Forward
0 new messages