[英]casper js how can i pass varibales from one homade function to another
好的,所以我已经开始构建自己的模块以在casperjs中使用,我已经到了要在其中将变量从一个函数传递给另一个函数的地步,这是我下面的内容
我的模块-功能
exports.accdata = function(accnum, amnum) {
var accountnumber = casper.fetchText('div.arabic:nth-child(2) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(' + accnum + ') > td:nth-child(2) > a:nth-child(1)');
var amountwithtype =casper.fetchText('div.arabic:nth-child(2) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(' + amnum + ') > td:nth-child(4) > div:nth-child(1)');
var redir = accountnumber.substr(1);
var split = amountwithtype.split (' ');
var amount = split[0];
var type = split[1];
};
exports.job = function (in1, in2){
console.log(in1);
console.log(in2);
};
然后我想通过的是我的casperjs脚本
casper.then(function(){
universe.accdata("3", "3");
universe.job(amount, type);
});
我是一个非常新的人,确实需要有关如何设置函数来传递数据的帮助。
您的accdata
函数只是设置一堆变量,然后不使用它们。 最简单的解决方案是将两个函数放在一起,但另一个解决方案可能是从第一个函数返回一个对象并将其作为参数传递给第二个函数。
exports.accdata = function(accnum, amnum) {
return {
accountnumber: casper.fetchText('div.arabic:nth-child(2) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(' + accnum + ') > td:nth-child(2) > a:nth-child(1)'),
amountWithType: casper.fetchText('div.arabic:nth-child(2) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(' + amnum + ') > td:nth-child(4) > div:nth-child(1)'),
...
};
};
exports.job = function(data) {
console.log(data);
};
然后像这样使用它:
casper.then(function() {
universe.job(universe.accdata("3", "3"));
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.