CasperJS无法在登录网站上工作

时间:2014-11-27 08:02:44

标签: javascript phantomjs casperjs

这是我的代码,我想打印出只有登录页面的元素值,但是我失败了。 https://mp.weixin.qq.com/cgi-bin/loginpage?t=wxm2-login&lang=zh_CN是我要登录的页面。

/**
 * Usage:  casperjs test.js 111111@qq.com password
 * # phantomjs --version
 * 1.9.7
 * # casperjs --version
 * 1.1.0-beta3

 */

var casper = require('casper').create({
    logLevel: 'debug',
    verbose: true,
    pageSettings: {
        loadImages: false, // do not load images
        loadPlugins: false // do not load NPAPI plugins (Flash, Silverlight, ...)
    },
    clientScripts:  [
        './jquery-1.11.1.min.js'
    ],

});

casper.userAgent('Mozilla/5.0 (Windows NT 6.1; rv:29.0) Gecko/20100101 Firefox/29.0');

// removing default options passed by the Python executable
casper.cli.drop("cli");
casper.cli.drop("casper-path");

if (casper.cli.args.length === 0 && Object.keys(casper.cli.options).length === 0) {
    casper.echo("No arg nor option passed").exit();
}


var usr = casper.cli.get(0);
var passwd = casper.cli.get(1);

casper.start().thenOpenAndEvaluate('https://mp.weixin.qq.com/', function() {
    var f = document.querySelector('form');
    f.querySelector('input[id="account"]').value = '111111@qq.com';
    f.querySelector('input[id="password"]').value = 'password';
    f.submit();
});


casper.then(function() {
   this.echo('Page url is ' + this.getCurrentUrl());
   this.echo('Page title is ' + this.getTitle());

});

casper.run();

我也尝试了其他登录方法,但也无法正常工作,我想知道该网站是否有一些禁止,否则为什么这不起作用:

// method 1 to login
casper.start('https://mp.weixin.qq.com/', function() { 
    this.fillSelectors('form#login-form', { 
        'input[id="account"]':  "1111@qq.com", 
        'input[id="password"]': passwd 
    }, false); 

    this.sendKeys('#account', '1111@qq.com');
    this.sendKeys('#password', "1111");
    this.click('#login_button');

    //this.sendKeys('input[id="account"]', "my_account");
    //this.sendKeys('input[id="password"]', "my_password");
    //this.click('#login_button');
});

// method 2 to login 
casper.start().thenOpen('https://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN', {
    method: "post",
    data: {
        f: 'json',
        imgcode: '',
        username: '1111@qq.com',
        pwd: '00fbd6d3e86be9c1f3cd2e7f22b17505'
    }
}, function() {
    this.echo("POST request has been sent.");
    this.echo('Page url is ' + this.getCurrentUrl());
    this.echo('Page title is ' + this.getTitle());
});

0 个答案:

没有答案