问题
I'm trying to login into Tumblr with CasperJS using methods start, fill and waitForUrl but cannot do this. It seems to me that form wasn't submitted, because timeout in waitForUr expired everytime.
Terminal response:
Code I'm using is this:
/*jslint browser: true, regexp: true */
// global casper, require
var LOGIN_URL, LOGIN_USERNAME, LOGIN_PASSWORD, casper;
casper = require('casper').create({
waitTimeout: 20000,
viewportSize: {
width: 1024,
height: 768
},
verbose: true,
logLevel: 'debug',
userAgent: 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)'
});
if (!casper.cli.has('username') && !casper.cli.has('password')) {
casper.echo('Usage: $ casperjs sfdclogin.casper.js --username=USERNAME --password=PASSWORD').exit(-1);
}
LOGIN_URL = 'https://tumblr.com/login';
LOGIN_USERNAME = casper.cli.get('username');
LOGIN_PASSWORD = casper.cli.get('password');
phantom.cookiesEnabled = true;
casper
.start(LOGIN_URL, function () {
// 'use strict';
this.log('Logging in', 'debug');
/*
this.fillSelectors('#signup_form_form', {
'#signup_email': LOGIN_USERNAME,
'#signup_password': LOGIN_PASSWORD
}, true);
*/
this.fill('#signup_form_form', {
'#user[email]': LOGIN_USERNAME,
'#user[password]': LOGIN_PASSWORD
}, true);
})
.waitForUrl('http://tumblr.com/dashboard', function () {
// 'use strict';
this.echo('We\'re logged in.');
})
.run();
Also, I'm using correct username and password with this script.
回答1:
Try using the following instead of your current casper.fill().
This is because CasperJS' fill() searches for the name attribute of elements.
this.fill('#signup_form', {
'user[email]': LOGIN_USERNAME,
'user[password]': LOGIN_PASSWORD
}, true);
You could use this if you want to continue using the ids as selectors.
this.fillSelectors('#signup_form', {
'#signup_email': LOGIN_USERNAME,
'#signup_password': LOGIN_PASSWORD
}, true);
来源:https://stackoverflow.com/questions/18609394/cannot-submit-form-with-casperjs-in-other-words-cannot-login