I have recently started learning CasperJS and I am trying to log in to YouTube. Here's my current code:
// XPathSignInButton is predefined
var Username = 'user123';
var Password = 'pass123';
casper.waitForSelector(xPathSignInButton, function() {
this.thenEvaluate(function() {
$('#Email').val(Username);
$('#Passwd').val(Password);
});
});
The code above does not manipulate the input fields (does not write in them), however if I replace thenEvaluate()
with this.fillSelectors()
, the variables are accessed normally and everything executes fine:
// XPathSignInButton is predefined
// signInFormSelector is predefined
casper.waitForSelector(xPathSignInButton, function() {
this.fillSelectors(signInFormSelector, {
'input[name="Email"]': Username,
'input[name="Passwd"]': Password
});
});
I first thought that there was a problem with how I called jQuery, but when I replaced the variables with the actual strings like: $('#Email').val('user123');
and $('#Email').val('pass123');
- it worked like a charm.
My question is how do I access variables from within the thenEvaluate()
function. Is it at all possible?