如何在casperjs中添加所有数字?

问题描述 投票:0回答:1

我想得到那么多定居点。

但它不起作用。

这段代码有什么问题?

我应该在哪里编辑这段代码?

我预期结果:总价值:2614

但总的结果是0。

casperjs代码是。

var test = 0;
var num1 = 0;
var num2 = 0;

var casper = require('casper').create({
    // verbose: true,
    logLevel: 'debug',
    // clientScripts: ["/vendor/jquery.min.js"],
    pageSettings: {
        loadImages: false,
        loadPlugins: true,
        encoding: 'utf8',
        colorizerType: 'Dummy',
        userAgent: 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36'
    }
});


casper.start();

casper.then(function() {
  casper.thenOpen('https://github.com/Medium/phantomjs', function(){
      msg = 'phantomjs : ' + (this.fetchText('#js-repo-pjax-container > div.container.new-discussion-timeline.experiment-repo-nav > div.repository-content > div.overall-summary.overall-summary-bottomless > div > div > ul > li.commits > a > span', true));
      num1 = parseInt(msg.replace(/[^0-9]/g,'')); // result : 390
  });

  casper.thenOpen('https://github.com/casperjs/casperjs', function(){
      msg = 'github : ' + (this.fetchText('#js-repo-pjax-container > div.container.new-discussion-timeline.experiment-repo-nav > div.repository-content > div.overall-summary.overall-summary-bottomless > div > div > ul > li.commits > a > span', true));
      num2 = parseInt(msg.replace(/[^0-9]/g,'')); // result : 2224
  });

  total = num1 + num2;
  // 390 + 2224 = 2614
  this.echo('total : ' + total);
  // but result is 0
});

Casper.润();

javascript casperjs
1个回答
0
投票

在打开页面之前评估您的echo语句。你需要将它与.then一起包装,它会起作用。

casper.then(function() {
    total = num1 + num2;
    // 390 + 2224 = 2614
    this.echo('total : ' + total);
    // but result is 0
})
© www.soinside.com 2019 - 2024. All rights reserved.