Reputation: 1159
I have been able to scroll to the end of my web page in order to load all the dynamic content. But after doing this I would like to evaluate the page in order to get some data. My problem is if I run anything different than a simple
casper.echo("something");
My code breaks and I don't get the deeded data. This is my code:
var casper = require('casper').create();
casper.start("http://mypageWithDynamicContentOnScroll.com");
var linksPrendas = [];
casper.waitForSelector('#idOfDivContainingDynamicContent',function(){
scrollNow();
});
var currentHeight;
var page = 1;
function scrollNow(){
casper.scrollToBottom();
casper.waitForSelector("#page"+page+"Height", function() {
casper.echo('scrolling...');
page++;
scrollNow();
},
function _onTimeout(){
});
}
var data = '';
casper.then(function(){
casper.echo('e');
//data = __utils__.findOne('div#someId').textContent;
casper.echo('f');
//var links = this.evaluate(function() {
// casper.echo("Evaluate ");
// var elements = __utils__.findAll('a');
// return elements. map (function (e) {
// return e.getAttribute('href');
// });
//});
});
casper.run(function(){
// var data = casper.evaluate(function() {
// var elements = __utils__.findAll('a.productListLink');
// return elements.map(function(e) {
// return e.getAttribute('href');
// });
// casper.echo("Evaluate ");
// return [1,2,3];
// });
casper.echo("Then");
casper.echo(elements);
casper.echo("DONE").exit();
});
You can see from some of my commented code that I have tried many different options without any success. I have also tried placing the evaluate or the findAll inside the _onTimeout callback. I either break the code and "DONE" is never printed or I never get to execute the code inside evaluate or anything else.
The scrolling works fine, and without the scrolling I can manage to evaluate the page and get the wanted content.
So how can I make this work? Evaluate the page after the scrolling is done?
Thank you
EDIT: I have also tested passing a callback function to scrollNow() to continue the execution after scrollNow() is called. The callback is called, but again, no luck with evaluate of findAll or any other function to get the wanted data.
Upvotes: 0
Views: 81
Reputation: 61952
The casper
object is only available outside of casper.evaluate
and __utils__
is only available inside of casper.evaluate
. Calling casper.echo(...)
will result in a TypeError and will stop the execution.
If you want to print something from the page context (inside of casper.evaluate
), then you need to register to the "remote.message"
event:
casper.on("remote.message", function(msg){
this.echo(msg);
});
...
casper.evaluate(function(){
console.log("something from the page");
});
Everything else looks fine.
Upvotes: 1