2017-06-26 82 views
1

我使用casperjs(在中间如此phantomjs)访问一些谷歌实用程序,但在访问它们之前,我们应该登录谷歌。对于V1谷歌认证,我们使用下面的脚本:Casperjs谷歌登录(V2)不工作

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"]': '[email protected]', 
    }); //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事件。什么都没有

有人有关于如何解决这个问题的想法?非常感谢!

回答

0

我也试图相同,我发现点击是与this.click('#identifierNext');和谷歌装载机开始工作。如果您在点击截屏后使用以下代码,您会看到加载器出现,但在此之后,而不是进入密码屏幕,它会回到电子邮件屏幕。

截图代码

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 
    }); 
}); 

但我也无法到达密码窗口,如果这方面的帮助,你可以跟任何想法,让我知道。

0

卡斯帕使用PhantomJS,而幻影本身无法登录谷歌帐户登录。它似乎使用了phantomjs中不支持的任何ES6功能,它以静默方式失败。

也许你可以有更多的运气与beta phantomjs 2.5。 无论如何,phantomjs不赞成使用无铬镀铬。正如幻影维护者维塔利Slobodin https://groups.google.com/forum/#!topic/phantomjs/9aI5d-LDuNE

说,好消息是,你可以在无头模式启动铬:/opt/google/chrome/chrome --headless --disable-gpu --repl,做任何你想要的。

可以在节点与--remote-debugging-port=9224更换--repl来控制它在任何远程代码,就像一个程序... 有库来控制它像phantomjs。 高级别(如幻影):https://github.com/GoogleChrome/puppeteer 下级拥有更多的控制:https://github.com/cyrus-and/chrome-remote-interface#clientdomainmethodparams-callback

目前我没有运气的木偶但铬远程接口,我能够在谷歌帐户登录。

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='[email protected]';"; 
    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-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