Casperjs Google登录(V2)不起作用



i都使用casperjs(中间的phantomjs(来访问某些Google实用程序,但是,在访问它们之前,我们应该登录Google。对于V1 Google身份验证,我们使用以下脚本:

var casper = require('casper').create();
url = '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';
casper.start(url, function() {
  this.fillSelectors('form#gaia_loginform', {
    'input[name="Email"]': 'your@email',
  }); //Fills the email box with email
  this.click("#next");
  this.wait(500, function() { //Wait for next page to load
    this.waitForSelector("#Passwd", //Wait for password box
      function success() {
        console.log("SUCCESS...");
        this.fillSelectors('form#gaia_loginform', {
          'input[name="Passwd"]': 'yourPassw',
        }); //Fill password box with PASSWORD
        this.click("#signIn"); //Click sign in button
        this.wait(500, function() {}); //Wait for it fully sigin
        casper.thenOpen('http://utility.google.com/', function() {
            this.wait(2000, function() {
                this.capture('media/status.png', undefined, {
                    format: 'png',
                    quality: 100`enter code here`
                });
            });
        });
      },
      function fail() {
        console.log("FAIL...");
      }
    );
  });
});
casper.run();

我们改变了操纵表格并填充字段的方式,到目前为止它正在运行。V2身份验证的问题在于,无法触发鼠标事件,这意味着我们无法单击使用this.click("#next"(和this.click("#signin"(。我尝试使用不同的鼠标事件在表单上进行发布,并尝试直接操纵JSACTION事件。没有什么可用。

有人对如何解决这个问题有一个想法?非常感谢!

casper使用phantomjs,而幻影本身无法在Google帐户登录中登录。它似乎使用了phantomjs中不支持的任何ES6功能,它会默默失败。

也许您可以使用Beta phantomjs 2.5有更多运气。无论如何,phantomjs被弃用而不是Chrome无头。如Phantom维护者Vitaly Slobodin所说https://groups.google.com/forum/# !! topic/phantomjs/9ai5d-ldune

好消息是,您可以以:/opt/google/chrome/chrome --headless --disable-gpu --repl并做任何您想做的事情。

您可以用--remote-debugging-port=9224替换--repl,以在任何远程代码中控制它,例如Node中的程序...有图书馆像phantomjs一样控制它。高级(例如幻影(:https://github.com/googlechrome/puppeteer较低的级别具有更多的控制力:https://github.com/cyrus-and/chrome-remote-interface#clientdomainmethodparams-callback

目前,我对Puppeteer没有运气

const CDP = require('chrome-remote-interface');
const argv = require('minimist')(process.argv.slice(2));
const file = require('fs');
// CLI Args
const url = argv.url || 'https://accounts.google.com';
const format = argv.format === 'jpeg' ? 'jpeg' : 'png';
const viewportWidth = argv.viewportWidth || 1440;
const viewportHeight = argv.viewportHeight || 900;
let delay = argv.delay || 0;
const userAgent = argv.userAgent;
const fullPage = argv.full;
// Start the Chrome Debugging Protocol
CDP(async function(client) {
  // Extract used DevTools domains.
  const {DOM, Emulation, Network, Page, Runtime} = client;
  // Enable events on domains we are interested in.
  await Page.enable();
  await DOM.enable();
  await Network.enable();
  // If user agent override was specified, pass to Network domain
  if (userAgent) {
    await Network.setUserAgentOverride({userAgent});
  }
  // Set up viewport resolution, etc.
  const deviceMetrics = {
    width: viewportWidth,
    height: viewportHeight,
    deviceScaleFactor: 0,
    mobile: false,
    fitWindow: false,
  };
  await Emulation.setDeviceMetricsOverride(deviceMetrics);
  await Emulation.setVisibleSize({width: viewportWidth, height: viewportHeight});
  // Navigate to target page
  await Page.navigate({url});
  // Wait for page load event to take screenshot
  Page.loadEventFired(async () => {
    // If the `full` CLI option was passed, we need to measure the height of
    // the rendered page and use Emulation.setVisibleSize
    if (fullPage) {
      const {root: {nodeId: documentNodeId}} = await DOM.getDocument();
      const {nodeId: bodyNodeId} = await DOM.querySelector({
        selector: 'body',
        nodeId: documentNodeId,
      });
      const {model: {height}} = await DOM.getBoxModel({nodeId: bodyNodeId});
      await Emulation.setVisibleSize({width: viewportWidth, height: height});
      // This forceViewport call ensures that content outside the viewport is
      // rendered, otherwise it shows up as grey. Possibly a bug?
      await Emulation.forceViewport({x: 0, y: 0, scale: 1});
    }
    let expr="document.querySelector('input[type=email]').value='YOUREMAIL@gmail.com';";
    expr+="document.querySelectorAll('div[role=button]')[0].click();";
    setTimeout
    let x=await Runtime.evaluate({expression: expr});
    console.log('******' + JSON.stringify(x));
    setTimeout(async function(){
    expr="document.querySelector('input[type=password]').value='YOUR_PASSWORD';";
    expr+="document.querySelectorAll('div[role=button]')[1].click()";
    x=await Runtime.evaluate({expression: expr});
    console.log('******' + JSON.stringify(x));
    x=await ( async function() {
            let expr="document.querySelector('input[type=password]')";
            return Runtime.evaluate({expression: expr});
    })()
    console.log('**' + JSON.stringify(x));
    }, 2000);
delay=5000
    setTimeout(async function() {
      const screenshot = await Page.captureScreenshot({format});
      const buffer = new Buffer(screenshot.data, 'base64');
      file.writeFile('output.png', buffer, 'base64', function(err) {
        if (err) {
          console.error(err);
        } else {
          console.log('Screenshot saved');
        }
        client.close();
      });
    }, delay);
  });
}).on('error', err => {
  console.error('Cannot connect to browser:', err);
});

参考:https://medium.com/@dschnr/using-headless-chrome-as-as-an-automated-screenshot-tool-4b07dffba79a

https://github.com/googlechrome/puppeteer/blob/master/docs/api.md#browserwsendpoint

https://developers.google.com/web/updates/2017/04/headless-chrome

我也尝试了同样的尝试,我发现点击正在使用this.click('#identifierNext');,而Google加载程序开始工作。如果您在单击屏幕截图后使用以下代码使用以下代码,则可以看到加载程序出现,但是在此之后,它不是进入密码屏幕,而是返回电子邮件屏幕。

屏幕截图代码

this.wait(200, function(){
    this.capture('1.jpg',{
        top: 0,
        left: 0,
        width: 4608, 
        height: 3456,
        quality:20
    });
});
this.wait(100, function(){
    this.capture('2.jpg',{
        top: 0,
        left: 0,
        width: 4608, 
        height: 3456,
        quality:20
    });
});
this.wait(100, function(){
    this.capture('3.jpg',{
        top: 0,
        left: 0,
        width: 4608, 
        height: 3456,
        quality:20
    });
});
this.wait(100, function(){
    this.capture('4.jpg',{
        top: 0,
        left: 0,
        width: 4608, 
        height: 3456,
        quality:20
    });
});

,但我也无法到达密码屏幕,如果有了这样的帮助,您可以告诉我任何想法。

最新更新