2017-06-26 78 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