casperjs 知乎登陆

phantom.casperTest = true;
phantom.outputEncoding="utf-8"; 
var fs = require('fs');

var casper = require('casper').create({
       // pageSettings: {
        //    loadImages:  true,
        //    loadPlugins: false
       // },
        logLevel: "info",
        verbose: false
    });
var fs=require('fs');
var url="https://www.zhihu.com/#signin";
casper.userAgent('Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.109 Safari/537.36');

casper.start(url, function () {   
    this.capture("1.png");   
    this.echo("启动程序....");   
});   

//输入登录信息   
casper.then(function () {   
    this.fill('form[novalidate="novalidate"]', {  
        "account": "user",   
        "password": "**************"   
    }, false);   
    this.capture("2.png");   
    this.echo("等待点击登录按钮");   
    fs.write("zhihu.html",this.getHTML(),'w');
   
});   
//点击登录按钮   
casper.waitForSelector(('button[class="sign-button submit"]'), function () {
    this.click('button[class="sign-button submit"]');  
    this.capture("4.png");   
    this.echo("已经点击登录按钮, 跳转等待.....");   
    this.wait(3000, function () {   
        this.echo(this.getTitle());   
        this.capture("5.png");   
        this.echo("登录成功");   
    });   
});   
casper.run(); 
  
//该代码片段来自www.oicqzone.com  

  

posted @ 2017-08-01 17:02  公众号python学习开发  阅读(193)  评论(0编辑  收藏  举报