问题
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:
- a cool link on javascript scoping
- 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