casperjs passing params to evaluate fails

我与影子孤独终老i 提交于 2019-12-21 07:56:34

问题


casper.then(function(){
 phone_number   = '7wqeqwe6';
 phone_password = 'Teqweqweqw34';

});



casper.thenEvaluate(function(phone,password) {

document.querySelector('input#myTMobile-phone').setAttribute('value',phone);
document.querySelector('input#myTMobile-password').setAttribute('value',password);

//  document.querySelector('form').submit();
}, { 

phone    : phone_number,
password : phone_password

});

this throws me

string(307) "[37;41;1mFAIL[0m ReferenceError: Can't find variable: phone_number

Is there a way to pass params to evaluate method?


回答1:


Try something like this:

var phone_number = '7wqeqwe6',
    phone_password = 'Teqweqweqw34';

casper.start('http://…');

casper.thenEvaluate(function(phone, password) {
    document.querySelector('input#myTMobile-phone').setAttribute('value', phone);
    document.querySelector('input#myTMobile-password').setAttribute('value', password);
    //  document.querySelector('form').submit();
}, {
    phone: phone_number,
    password: phone_password
});

Notes:

  1. a cool link on javascript scoping
  2. filling forms? there's an API for that



回答2:


The other answers are pre 1.0. The preferred way is to pass along the arguments in line

Example

casper.evaluate(function(username, password) {
    document.querySelector('#username').value = username;
    document.querySelector('#password').value = password;
    document.querySelector('#submit').click();
}, 'sheldon.cooper', 'b4z1ng4');

http://docs.casperjs.org/en/latest/modules/casper.html#evaluate




回答3:


I don't know whats wrong with your code. Have a look at the CasperJS API:

Evaluates an expression in the remote page context, a bit like what PhantomJS' WebPage#evaluate does, but can also handle passed arguments if you define their context:

Example:

casper.evaluate(function(username, password) {
    document.querySelector('#username').value = username;
    document.querySelector('#password').value = password;
    document.querySelector('#submit').click();
}, {
    username: 'sheldon.cooper',
    password: 'b4z1ng4'
});


来源:https://stackoverflow.com/questions/13253824/casperjs-passing-params-to-evaluate-fails

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