无法使用CasperJS提交表单 - 换句话说无法登录

时间:2013-09-04 08:53:53

标签: javascript node.js tumblr casperjs

我正在尝试使用方法startfillwaitForUrl使用CasperJS登录Tumblr,但不能执行此操作。 在我看来,表单未提交,因为waitForUr中的超时每次都会过期。

终端响应:

我正在使用的代码是:

/*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();

,我在这个脚本中使用了正确的用户名和密码。

1 个答案:

答案 0 :(得分:6)

尝试使用以下内容代替您当前的casper.fill()。 这是因为CasperJS'fill()搜索元素的name属性。

this.fill('#signup_form', {
    'user[email]': LOGIN_USERNAME,
    'user[password]': LOGIN_PASSWORD
}, true);

如果你想继续使用id作为选择器,你可以使用它。

this.fillSelectors('#signup_form', {
    '#signup_email': LOGIN_USERNAME,
    '#signup_password': LOGIN_PASSWORD
}, true);