使用附加配置创建一个 casper 实例,例如注入 jQuery

Creating a casper instance with additional configuration such as injecting jQuery

我想在 CasperJS 中使用 jQuery,但似乎无法将 jQuery 注入远程文档。

下面的代码是简化的代码,用于显示可能存在问题的位置。

我知道我需要稍后使用 casper.evaluate 但我无法注入 jQuery.

我应该如何更改代码才能正确注入 jQuery?

此代码有效

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

casper.start('http://www.yahoo.co.jp', function() {

    this.echo("hello");

});

此代码无效

var casper = require('casper');
casper.create({
    clientScripts: ['jquery-2.2.2.min.js']
});

casper.start('http://www.yahoo.co.jp', function() {

    this.echo("hello");

});

当前目录和jQuery文件所在的位置

$ pwd
/Users/Hayato/Desktop

$ ls | grep "jquery"
jquery-2.2.2.min.js

这与注入无关jQuery。您混淆了如何获取 casper 实例。

使用其中之一

var casper = require('casper').create({
    clientScripts: ['jquery-2.2.2.min.js']
});

var casper = require('casper');
casper = casper.create({
    clientScripts: ['jquery-2.2.2.min.js']
});

问题是 create() 为您提供了可以使用的实例,但您目前正在丢弃它。