narek
narek

Reputation: 1036

casperjs passing params to evaluate fails

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?

Upvotes: 10

Views: 9992

Answers (3)

fracmak
fracmak

Reputation: 121

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

Upvotes: 10

NiKo
NiKo

Reputation: 11414

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

Upvotes: 22

mike
mike

Reputation: 880

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

Upvotes: 1

Related Questions