我如何在CasperJS中使用jQuery? [英] How Do I use jQuery in CasperJS?

查看:296
本文介绍了我如何在CasperJS中使用jQuery?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

casper.start(URL, function() {

    casper.page.injectJs('C:/Users/Mike/Documents/n1k0-casperjs-bc0da16/jquery-1.10.2.min.js');
    var names = $('span.author-name');
    this.echo(names);
    this.exit();
}

ReferenceError:找不到变量:$

ReferenceError: Can't find variable: $

我该怎么办?我在创建casper实例时也试过这个:

What do I do? I've tried this too when creating the casper instance:

var casper = require('casper').create({

    // I've tried both commented lines below

    // clientScripts: ['C:/Users/Mike/Documents/n1k0-casperjs-bc0da16/jquery-1.10.2.min.js']
    // clientScripts: ['includes/jquery-1.10.2.min.js']
});


推荐答案

您已经评估了jQuery代码浏览器上下文使用 casper.evaluate

You have evaluate the jQuery code in the browser context using casper.evaluate


执行代码,就像使用浏览器控制台一样。

execute code as if you were using the browser console.



var nameCount = this.evaluate(function() {
    var names = $('span.author-name')
    return names.length;
});
this.echo(nameCount);

这篇关于我如何在CasperJS中使用jQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆