如何使用CasperJS登录网站? [英] How to login into a website with CasperJS?

查看:202
本文介绍了如何使用CasperJS登录网站?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过提交表单登录CasperJS。我搜索谷歌并没有找到任何关于它的好例子。

How can I login with CasperJS by submitting a form. I searched google and haven't found any good examples about it.

推荐答案

你需要使用Casper fill()功能。

You will need to use Casper fill() function.

以下是登录Facebook并在登录后打印出您的姓名的示例。请注意,您需要输入您的用户名和密码:

Below is an example which login to Facebook and print out your name after login. Note that you need to put in your username and password:

var casper = require('casper').create({   
    verbose: true, 
    logLevel: 'debug',
    pageSettings: {
         loadImages:  false,         // The WebPage instance used by Casper will
         loadPlugins: false,         // use these settings
         userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4'
    }
});

// print out all the messages in the headless browser context
casper.on('remote.message', function(msg) {
    this.echo('remote message caught: ' + msg);
});

// print out all the messages in the headless browser context
casper.on("page.error", function(msg, trace) {
    this.echo("Page Error: " + msg, "ERROR");
});

var url = 'http://www.facebook.com/';

casper.start(url, function() {
   console.log("page loaded");
   this.test.assertExists('form#login_form', 'form is found');
   this.fill('form#login_form', { 
        email: '**<put your email here>**', 
        pass:  '**<put your password here>**'
    }, true);
});

casper.thenEvaluate(function(){
   console.log("Page Title " + document.title);
   console.log("Your name is " + document.querySelector('.headerTinymanName').textContent ); 
});

casper.run();

这篇关于如何使用CasperJS登录网站?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆