CasperJS passing variable to evaluate can't get it to work [duplicate]

前提是你 提交于 2019-11-27 08:12:21

问题


This question already has an answer here:

  • Passing variable into page.evaluate - PhantomJS 3 answers

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.


回答1:


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

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

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


来源:https://stackoverflow.com/questions/22597987/casperjs-passing-variable-to-evaluate-cant-get-it-to-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!