首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PhantomCSS屏幕截图在for循环内部不正确工作

PhantomCSS屏幕截图在for循环内部不正确工作
EN

Stack Overflow用户
提问于 2016-12-05 11:02:30
回答 2查看 113关注 0票数 0

我想在每一页上捕捉一张截图。为了导航到不同的页面,有一个函数moveNext()。当我签入控制台时,我可以看到它按顺序导航到所有页面。然而,它不是同时在每一页截图,而是采取多个截图的最后一页。提供回调或等待选项吗?

代码语言:javascript
复制
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);
            }
        });
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-12-21 09:57:03

代码语言:javascript
复制
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');
                });
            });
        });
}
票数 0
EN

Stack Overflow用户

发布于 2016-12-06 05:24:18

for循环不能工作,因为它太快了。你可以使用这样的东西:

代码语言:javascript
复制
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();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40972740

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档