首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CasperJs Google登录

CasperJs Google登录
EN

Stack Overflow用户
提问于 2017-06-15 00:54:43
回答 2查看 709关注 0票数 0

我一直在编写一些代码来访问我的google CSE,因此我需要使用我的google帐户登录。

我有以下代码:

代码语言:javascript
复制
var casper = require('casper').create({
    verbose: true,
   logLevel: 'debug',
   waitTimeout: 5000,
   clientScripts: ["libs/jquery.min.js"],
   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'
});



const google_email = "MY_EMAIL";
const google_passwd = "MY_PASSWORD";
const loginUrl = 'https://accounts.google.com';


casper.start(loginUrl, function() {
    this.waitForSelector('#view_container > form');
});


casper.then(function() {
    this.fillSelectors('#view_container > form', {
    'input[name="identifier"]': google_email
    }, false);
});
casper.then(function() {
    this.click('#identifierNext');
});

casper.wait(500, function() { //Wait for next page to load
    this.capture('images/step01.png');
});

casper.then(function() {
    this.evaluate(function () {
        var identifierNext = $('#identifierNext');
        identifierNext.click();
    });
});

casper.then(function() {
    this.capture('images/step02.png');
});

 casper.run(function() {
     this.echo("Done");
 });

输入电子邮件的部分似乎起作用了。

但是点击部分不起作用。

我找到了this,但它似乎过时了。

谢谢

EN

回答 2

Stack Overflow用户

发布于 2017-06-26 23:58:28

我还没有修复与新表单相关的问题,但我们找到了访问旧表单的方法,因此“旧”脚本应该仍然有效,该解决方案是禁用JS。为此,请将loginUrl更改为

https://accounts.google.com/ServiceLogin?passive=1209600&continue=https%3A%2F%2Faccounts.google.com%2FManageAccount&followup=https%3A%2F%2Faccounts.google.com%2FManageAccount&flowName=GlifWebSignIn&flowEntry=ServiceLogin&nojavascript=1#identifier

重要的是: nojavascript=1

我们使用的是Casperjs Google Login Not working中发布的脚本,只是更改了登录地址。

票数 1
EN

Stack Overflow用户

发布于 2018-09-27 12:57:43

试试这个--等待动态加载的页面需要更多的延迟。

代码语言:javascript
复制
casper.options.verbose = true; // verbose reporting
casper.options.logLevel = 'debug'; // full log reporting
casper.options.exitOnError = false; // Keep going on error

const google_email = "EMAIL";
const google_passwd = "PASSWORD";
const loginUrl = 'https://accounts.google.com';

// Load the login page
casper.start(loginUrl, function() {
    this.waitForSelector('#view_container'); // '> form' doesn't seem to work
});

// Fill in the 'username' form
casper.then(function() {
    this.fill('form', {
        identifier: google_email,
    });
    this.sendKeys('#view_container', casper.page.event.key.Enter , {keepFocus: true});
});

// First 'Enter' is too quick for Google, send another one after a pause
casper.wait(2500, function() {
    this.sendKeys('#identifierId', casper.page.event.key.Enter , {keepFocus: true});
});

// Wait for the 'password' form
casper.waitForSelector("#passwordNext", function() {
    this.echo("password form is apparently available");
});

// Password form seems to load slowly, even if the selector is found/visible, this delay ensures next form fill works
casper.wait(2500, function() {
    this.echo("password form is really available");
});

// Fill in the 'password' form
casper.then(function() {
    this.fill('form', {
        password: google_passwd,
    });
    this.sendKeys('#view_container', casper.page.event.key.Enter , {keepFocus: true});
});

// First 'Enter' is too quick for Google, send another one after a pause
casper.wait(500, function() {
    this.sendKeys('input.whsOnd.zHQkBf', casper.page.event.key.Enter , {keepFocus: true});
});

// Extend timeout to allow for slow dynamic page rendering
casper.options.waitTimeout = 25000;
casper.waitForSelector("#gb", function() {
    this.echo("login complete");
});

casper.thenOpen('https://(google app you want)', function() {
    // Check it opened okay
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44550584

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档