0

我想在每个页面上截取屏幕截图。为了导航到不同的页面,有一个功能moveNext()。当我签入控制台时,我可以看到它按顺序导航到所有页面。但是,它不是在每个页面同时截屏,而是对最后一页进行多张截屏。casperjs 是否提供回调或等待选项?

casper.then(function () {
            for (var currentPage = startPage; currentPage < lastPage; currentPage++) {              
                this.page.evaluate(function(currentPage) {
                    moveNext(currentPage);
                }, currentPage);
                phantomcss.screenshot('html', 'screenshot');
                console.log(currentPage);
            }
        });
4

2 回答 2

0

循环不起作用,for因为它太快了。你可以使用类似的东西:

var casper = require('casper').create({
    verbose: true,
    logLevel: 'debug',
    viewportSize:{width: 1600, height: 900}
});
var currentPage = 0,lastPage=5;
casper
     .start()
            .then(function to_call() {
                      if(currentPage<lastPage){
                         currentPage++;
                       /*  this.page.evaluate(function(currentPage) {
                         moveNext(currentPage);
                         }, currentPage);
                         phantomcss.screenshot('html', 'screenshot');*/
                         this.echo("The currentPage number is: "+currentPage,"INFO");
               this.wait(3000).then(to_call)
               }

        })

 .run();
于 2016-12-06T05:24:18.200 回答
0
function captureScreenshot(width, height, device, startPage, lastPage) {
        casper.viewport(width, height);

        casper.then(function () {
            var currentPage = startPage - 1;

            /* Capture screenshot of footer */
            phantomcss.screenshot('footer', 'footer');

            /* Capture screenshot of header */
            phantomcss.screenshot('header', 'header');

            /* Capture main content screenshot */
            casper.repeat(lastPage, function () {
                this.page.evaluate(function (currentPage) {
                    moveNext(currentPage);
                }, ++currentPage);

                this.wait(8000, function () {
                    phantomcss.screenshot('#NavForm > .container', 'main-content');
                });
            });
        });
}
于 2016-12-21T09:57:03.587 回答