简体   繁体   English

CasperJS-使用jQuery。 ReferenceError:找不到变量:jQuery / $

[英]CasperJS - using jQuery. ReferenceError: Can't find variable: jQuery/$

I'm writing code that involves jQuery in CasperJS. 我正在CasperJS中编写涉及jQuery的代码。 By chance, could someone point out the error I've made in including jQuery? 偶然地,有人可以指出我在包含jQuery时所犯的错误吗? (After 45 minutes of searching, I'm starting to think it's a local problem.) (经过45分钟的搜索,我开始认为这是本地问题。)

I have tried both of the following: 我已经尝试了以下两种方法:

casper.page.injectJs('C:\sweeps\jquery-1.10.2.min.js');

and

var casper = require('casper').create({
clientScripts: ["C:\sweeps\jquery-1.10.2.min.js"]
});

Code: 码:

// sample.js
var casper = require('casper').create();

var login = "some username"; 
var password = "some password";

casper.start('https://www.paypal.com/us/home', function() {
    this.fillXPath('form.login', {
        '//input[@name="login_email"]':    login,
        '//input[@name="login_password"]':    password,
    }, true);
});

casper.page.injectJs('C:\sweeps\jquery-1.10.2.min.js');

$("input[name='submit.x']").click();

   setTimeout(function(){ 
   setTimeout(function(){ 

casper.run(function() {

this.captureSelector('example2.png', '#page');

    this.echo('Done.').exit();

});

}, 30000); }, 1);

Output: 输出:

ReferenceError: Can't find cariable: jQuery
C:/sweeps/test2.js:21

The same result comes when "jQuery" is switched to "$". 当“ jQuery”切换为“ $”时,也会出现相同的结果。

EDIT: I've also tried relative pathing. 编辑:我也尝试过相对路径。

My reference is: Can I use jQuery with CasperJS? 我的参考是: 我可以在CasperJS中使用jQuery吗?

Read this Casper#evaluate() 阅读此Casper#evaluate()

The concept behind this method is probably the most difficult to understand when discovering CasperJS. 发现CasperJS时,这种方法背后的概念可能是最难理解的。 As a reminder, think of the evaluate() method as a gate between the CasperJS environment and the one of the page you have opened; 提醒一下,将Evaluate()方法看作是CasperJS环境和您打开的页面之一之间的大门。 everytime you pass a closure to evaluate(), you're entering the page and execute code as if you were using the browser console. 每次将闭包传递给valuate()时,您都在进入页面并执行代码,就像使用浏览器控制台一样。

casper.evaluate(function() {
    $("input[name='submit.x']").click();
});

You need to use the jQuery selector as if you were in a browser. 您需要像使用浏览器一样使用jQuery选择器。

Your path to the javascript file should be a URI relative to your HTML file, not a file-system path. 您指向javascript文件的路径应该是相对于HTML文件的URI,而不是文件系统路径。 Assuming your files are in the c:\\sweepstakes folder, try 假设您的文件位于c:\\ sweepstakes文件夹中,请尝试

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

Also, use your browser's network/dev tools to see if your jQuery library is being downloaded or not. 另外,使用浏览器的网络/开发工具查看是否正在下载jQuery库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM