我试图让casperjs处理以下情况:
加载网页,然后在该页面中,ajax加载数据项 随着“阅读更多”'按钮,它反过来加载更多的数据 项目
我需要脚本以递归方式检查“更多”内容。按钮存在(因为要加载许多数据项),如果是,请单击它,否则继续使用脚本的其余部分并将整页输出为jpeg。
我已经尝试过编写下面的代码,但它并没有像我希望的那样循环。它只需单击按钮一次,然后输出图像,即使加载了更多数据,按钮仍然存在以便再次点击。
var casper = require('casper').create({
verbose : true,
logLevel : 'debug',
pageSettings : {
"userAgent" : 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.10 (KHTML, like Gecko) Chrome/23.0.1262.0 Safari/537.10',
"loadImages" : false,
"webSecurityEnabled" : false,
"ignoreSslErrors" : true
},
});
var urls = {
login : 'http://www.website.com/login',
details : 'http://www.website.com/details',
};
casper.start(urls.login, function() {
//login stuff
});
//the function I'm trying to recursively loop before moving on to saving the capture image
function checkMore() {
//check to see if the 'read more' button exists
if (casper.exists('a.read-more')) {
//click the button to load more items
casper.click('a.read-more');
//wait for the items to load, then run the check again
casper.wait(3000, function() {
casper.run(checkMore);
});
}
}
casper.thenOpen(urls.details, function() {
//wait for the page along with ajax items to load shortly after
this.wait(3000, function() {
this.run(checkMore);
});
});
casper.then(function() {
//output the result
this.capture('output.jpg');
});
casper.run();
答案 0 :(得分:0)
Pius有正确的想法,我修改了我的代码,解决方案如下。
var urls = {
login : 'http://www.website.com/login',
details : 'http://www.website.com/details',
};
casper.start(urls.login, function() {
//login stuff
});
//the function I'm trying to recursively loop before moving on to saving the capture image
function checkMore() {
//check to see if the 'read more' button exists
if (casper.exists('a.read-more')) {
//click the button to load more items
casper.click('a.read-more');
//wait for the items to load, then run the check again
casper.wait(3000, checkMore);
}
}
casper.thenOpen(urls.details, function() {
//wait for the page along with ajax items to load shortly after
this.wait(3000, checkMore);
});
casper.then(function() {
//output the result
this.capture('output.jpg');
});
casper.run();