javascript – CasperJS绑定问题

我试图达到一个instagram页面,但没有运气.我不断收到错误一个空白的屏幕截图.

错误文字

TypeError: 'undefined' is not a function (evaluating 'a.createDescriptor.bind(null,t)')

Casperjs –version为1.1.0-beta3.

基本上我使用以下代码

var casper = require('casper').create({
    verbose: true,logLevel: 'debug',pageSettings: {
         userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.4 (KHTML,like Gecko) Chrome/22.0.1229.94 Safari/537.4'
    },loadplugins: true
});

casper.on( 'page.error',function (msg,trace) {
    this.echo( 'Error: ' + msg,'ERROR' );
});

casper.start('http://instagram.com/hello',function() {
    casper.wait(3000,function()  {
        this.capture('screen.png');
    });
});

casper.run(function() {
    this.exit();
});

解决方法

如果使用PhantomJS 2,下面的垫片就不再需要了.可悲的是,CasperJS 1.1-beta3还不支持,所以您可能想要使用 GitHub的主分支.

问题是PhantomJS v1.x不支持Function.prototype.bind.你需要添加一个垫片.在CasperJS中,它进入了page.initialized事件处理程序. This shim对我来说非常有用:

casper.on( 'page.initialized',function(){
    this.evaluate(function(){
        var isFunction = function(o) {
          return typeof o == 'function';
        };

        var bind,slice = [].slice,proto = Function.prototype,featureMap;

        featureMap = {
          'function-bind': 'bind'
        };

        function has(feature) {
          var prop = featureMap[feature];
          return isFunction(proto[prop]);
        }

        // check for missing features
        if (!has('function-bind')) {
          // adapted from Mozilla Developer Network example at
          // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind
          bind = function bind(obj) {
            var args = slice.call(arguments,1),self = this,nop = function() {
              },bound = function() {
                return self.apply(this instanceof nop ? this : (obj || {}),args.concat(slice.call(arguments)));
              };
            nop.prototype = this.prototype || {}; // Firefox cries sometimes if prototype is undefined
            bound.prototype = new nop();
            return bound;
          };
          proto.bind = bind;
        }
    });
});

如果将垫片导出到自己的文件中并通过clientScripts选项包含,那么它不起作用,因为那些在后来的javascript之后被追加了太晚了.

注册page.resource.received事件也可能有效.

还有纯PhantomJS问题:bind polyfill for PhantomJS

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...