本文介绍了CasperJS传递变量进行评估无法使其工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧所以这是我的casperjs函数:

okay so here's my casperjs function :

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(); 

}

由于某些原因,我无法将变量输入到正确发送到此函数:

For some reason i just can't get the variable to send properly to this function :

    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 

    }); 

任何人都可以帮我解决这个问题,以便正确地传递数字来评估函数。

Can anyone help me fix this so the number actualy gets pass properly to evaluate function.

推荐答案

所需的变量作为从PhantomJS派生的函数。

Pass your needed variable as an argument of the evaluate() function derived from PhantomJS's evaluate().

您混合了两种不同的上下文。在页面DOM环境中(在 evaluate()内),所需未知,因为评估()是沙盒。

You mix the 2 different contexts. In the page DOM environment (inside evaluate()), needed is unknown, because evaluate() is sandboxed.

我设置 var neededCasperContext = needed; 给你看区别,但当然你可以直接传递它。

I set var neededCasperContext = needed; to show you the difference, but of course you can pass it directly.

var neededCasperContext = needed;

this.evaluate(function(neededPageDomContext) {

    document.getElementById('account').value = '6028';
    document.getElementsByName('data')[0].value = neededPageDomContext;
}, neededCasperContext);

这篇关于CasperJS传递变量进行评估无法使其工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 08:16