CasperJS Capture期间出现Javascript错误

时间:2015-01-09 21:20:30

标签: javascript phantomjs casperjs

我正在尝试使用CasperJS截取我的Twitter Analytics页面的截图。

该脚本成功登录Twitter并重定向到https://analytics.twitter.com/user/USERNAME,但只要加载该页面,我就会收到与TypeError: 'undefined' is not a function (evaluating 'function(t,e){s&&s.isAttachedTo(e)||(new this).initialize(e,i)}.bind(this)')相关联的错误https://ton.twimg.com/insights/js/en-app-175067a4b13730ff0a223fa1c6009fde.js。屏幕截图仍会捕获页面,但是as you can see,页面的许多元素都没有加载,并且显示错误。关于这里发生了什么的任何想法?如何加载完整页面以进行捕获?

我的CasperJS脚本:

var casper = require("casper").create({
    viewportSize: {
        width: 1024,
        height: 768
    },
    logLevel: "debug",
    verbose: true
});

var errors = [];

casper.start();

var utils = require('utils');

var resourcesTime = [];

casper.on('resource.requested', function(resource) {
    var date_start = new Date();

    resourcesTime[resource.id] = {
        'id': resource.id,
        'id_received': '',
        'start': date_start.getTime(),
        'end': -1,
        'time': -1,
        'status': resource.status,
        'url': resource.url,
        'url_received': ''
    };
});

casper.on('resource.received', function(resource) {
    var date_end = new Date();

    resourcesTime[resource.id]['end']  = date_end.getTime();
    resourcesTime[resource.id]['time'] = resourcesTime[resource.id]['end'] - resourcesTime[resource.id]['start'];
    resourcesTime[resource.id]['id_received']  = resource.id;
    resourcesTime[resource.id]['url_received'] = resource.url;

});

casper.open('https://twitter.com/login?redirect_after_login=https%3A//analytics.twitter.com/user/USERNAME/tweets').then(function() {
    if (this.exists('.signin')) {
        this.fill('.signin', {
            'session[username_or_email]': 'USERNAME',
            'session[password]': 'PASSWORD'
        }, true);
    }
    this.wait(5000, function() {
        this.capture("tweetshot.png");
        this.echo("Saved screenshot of " + (this.getCurrentUrl()));
    });
});

casper.on("page.error", function(msg, trace) {
  this.echo("Error:    " + msg, "ERROR");
  this.echo("file:     " + trace[0].file, "WARNING");
  this.echo("line:     " + trace[0].line, "WARNING");
  this.echo("function: " + trace[0]["function"], "WARNING");
  errors.push(msg);
});

casper.run(function() {
  if (errors.length > 0) {
    this.echo(errors.length + ' Javascript errors found', "WARNING");
  } else {
    this.echo(errors.length + ' Javascript errors found', "INFO");
  }
  //utils.dump(resourcesTime);
  casper.exit();
});

日志:

[info] [phantom] Starting...
[debug] [phantom] opening url: https://twitter.com/login?redirect_after_login=https%3A//analytics.twitter.com/user/USERNAME/tweets, HTTP GET
[debug] [phantom] Navigation requested: url=https://twitter.com/login?redirect_after_login=https://analytics.twitter.com/user/USERNAME/tweets, type=Other, willNavigate=true, isMainFrame=true
[info] [phantom] Running suite: 1 step
[debug] [phantom] url changed to "https://twitter.com/login?redirect_after_login=https://analytics.twitter.com/user/USERNAME/tweets"
[debug] [phantom] Navigation requested: url=about:blank, type=Other, willNavigate=true, isMainFrame=false
[debug] [phantom] Navigation requested: url=about:blank, type=Other, willNavigate=true, isMainFrame=false
[debug] [phantom] Navigation requested: url=about:blank, type=Other, willNavigate=true, isMainFrame=false
[debug] [phantom] Navigation requested: url=about:blank, type=Other, willNavigate=true, isMainFrame=false
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step anonymous 1/1 https://twitter.com/login?redirect_after_login=https://analytics.twitter.com/user/USERNAME/tweets (HTTP 200)
[info] [remote] attempting to fetch form element from selector: '.signin'
[debug] [remote] Set "session[username_or_email]" field value to USERNAME
[debug] [remote] Set "session[password]" field value to *************
[info] [remote] submitting form to https://twitter.com/sessions, HTTP POST
[info] [phantom] Step anonymous 1/1: done in 1742ms.
[info] [phantom] Step _step 2/2 https://twitter.com/login?redirect_after_login=https://analytics.twitter.com/user/USERNAME/tweets (HTTP 200)
[info] [phantom] Step _step 2/2: done in 1773ms.
[debug] [phantom] Navigation requested: url=https://twitter.com/sessions, type=FormSubmitted, willNavigate=true, isMainFrame=true
[debug] [phantom] Navigation requested: url=https://analytics.twitter.com/user/USERNAME/tweets, type=FormSubmitted, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "https://analytics.twitter.com/user/USERNAME/tweets"
Error:    TypeError: 'undefined' is not a function (evaluating 'function(t,e){s&&s.isAttachedTo(e)||(new this).initialize(e,i)}.bind(this)')
file:     https://ton.twimg.com/insights/js/en-app-175067a4b13730ff0a223fa1c6009fde.js
line:     4
function: l
Error:    TypeError: 'undefined' is not a function (evaluating 'function(t,e){s&&s.isAttachedTo(e)||(new this).initialize(e,i)}.bind(this)')
file:     https://ton.twimg.com/insights/js/en-app-175067a4b13730ff0a223fa1c6009fde.js
line:     4
function: l
[info] [phantom] wait() finished waiting for 5000ms.
[debug] [phantom] Capturing page to /Users/admin/Desktop/tweetshot.png
[info] [phantom] Capture saved to /Users/admin/Desktop/tweetshot.png
[debug] [phantom] Successfully injected Casper client-side utilities
Saved screenshot of https://analytics.twitter.com/user/USERNAME/tweets
[info] [phantom] Done 2 steps in 9363ms
2 Javascript errors found

谢谢

0 个答案:

没有答案