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?
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');
});