0

I am trying to scrape a web page. There is an ajax button (a div) on site, when clicked it appends the list on page with more results (like showing 20 more results). I want to click it 3 times.

Using the code below:

casper.then(function() {
        for(var i=1; i<=3; i++){
            casper.evaluate(function(){
                $("div.showMore").click();
                return true;
            })
            casper.wait(5000, function then(){
                this.capture('image.png');
            })
        }
})

but it just captures the page without the click. I am sure that the this code clicks..

$("div.showMore").click();

I checked it by pushing/trying it via chrome console.

So what am i missing?

1 Answers1

0

JavaScript is crazy with it's asynchronous nature. I would specifically spell out your clicks in one function to the next. It's not the most ideal solution but most likely the way your code is working is Casper is taking the screenshot either before or during the for loop. so your changes are not being registered. Try spelling it out into four different casper functions and see what the outcome is.

casper.then(function() {
        this.evaluate(function(){
            $("div.showMore").click();
        });

casper.wait(3000, function() {
        this.evaluate(function(){
            $("div.showMore").click();
        });

casper.wait(3000, function() {
        this.evaluate(function(){
            $("div.showMore").click();
        });

casper.wait(5000, function then(){
            this.capture('image.png');
        });
Chris Hawkes
  • 11,923
  • 6
  • 58
  • 68
  • Thx for the answer. But unfortunately it doesn't help. What i noticed that, it even doesn't work without the loop. `$("div.showMore").trigger('click');` didn't help either.. Or `document.querySelectorAll(".showMore")[0].click();` It seems that none is clicking the button. Any ideas? – user3294828 Mar 17 '14 at 21:46
  • Just to note: All of the mentioned click methods work via Chrome console. May be CasperJS bug? – user3294828 Mar 17 '14 at 21:47
  • I think it can be finicky sometimes, the best approach I have for clicking is using casper.click(x('with_xpath_here')); it's pretty fail proof. – Chris Hawkes Mar 18 '14 at 12:52
  • It was the jquery conflict. I got help from this post:http://stackoverflow.com/questions/14098483/casperjs-click-event-having-ajax-call – user3294828 Apr 04 '14 at 20:58