好的,所以这是我的casperjs功能:
if(casper.exists(ac2)){
var accountnumber = this.fetchText('div.arabic:nth-child(2) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(2) > td:nth-child(2) > a:nth-child(1)');
var redir = accountnumber.substr(1);
casper.then(function() {
var uel = "https://example.ws/send.html?f=" + redir;
this.thenOpen(uel, function() {
casper.wait(10000, function() {
casper.then(function() {
var accountnumber1 = this.fetchText('div.arabic:nth-child(1) > font:nth-child(1)');
var acccc = accountnumber1.split(' ');
system.stdout.writeLine(acccc[3]); // this output a number
var amount = acccc[3];
var result = amount * 0.019;
var result2 = result.toFixed(6);
var fresult = amount - result2;
var needed = fresult.toFixed(3);
system.stdout.writeLine(needed); // this output a number
this.evaluate(function() {
document.getElementById('account').value = '6028';
document.getElementsByName('data')[0].value = needed; // this just does not work even though i know there a number needed in var needed
});
//this.click("input#sbt.button[type='submit']");
casper.wait(10000, function() {
casper.then(function() {
this.capture("capture1.jpg");
var el2 = this.getHTML();
fs.write('result.html', el2, 'w');
});
});
});});
});
});
} else {
this.exit();
}
由于某种原因,我无法将变量正确发送到此函数:
this.evaluate(function() {
document.getElementById('account').value = '6028';
document.getElementsByName('data')[0].value = needed; // this just does not work even though i know there a number needed in var needed
});
任何人都可以帮我解决这个问题,以便数字实际通过以评估功能。
答案 0 :(得分:11)
将您的needed
变量作为派生自PhantomJS evaluate()
的evaluate()
函数的参数传递。
你混合了两种不同的背景。在页面DOM环境(evaluate()
内)中,needed
未知,因为evaluate()
是沙箱。
我设置var neededCasperContext = needed;
以显示差异,但当然您可以直接传递它。
var neededCasperContext = needed;
this.evaluate(function(neededPageDomContext) {
document.getElementById('account').value = '6028';
document.getElementsByName('data')[0].value = neededPageDomContext;
}, neededCasperContext);