Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I sign into Google using Puppeteer?

I am using Puppeteer and I am trying to sign into my Gmail account

URL: https://accounts.google.com/ServiceLogin/identifier?service=mail&passive=true&rm=false&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1&osid=1&flowName=GlifWebSignIn&flowEntry=AddSession

Currently my code types into the email form and submits enter, then when the page goes to the password screen, there is not way to write in the input for password. This may be because it is technically not a new page but the same. Either way I cannot seem to interact with this new page when I press enter on the email page.

I tried used a lot of the methods but to no avail.

const elementHandle = await page.$('input');
await elementHandle.type('[email protected]');
  await page.click("#identifierNext");

//goes to new page
//this code does not work. 
const pw = await page.$('input[type="password"]');


page.on('load', () => console.log("Loaded: " + page.url()));

})

like image 235
DavidCastro Avatar asked Feb 01 '18 21:02

DavidCastro


People also ask

How do I log into my puppeteer Google account?

Filling in Google login form The first version of the code should navigate to the login link, fill the email field, click the blue button Next and enter the password into the password field. ]); Note, the username and password are passed to the script as environment variables GUSER and GPASS .

How do I use puppeteer in Chrome?

By default, Puppeteer downloads and uses a specific version of Chromium so its API is guaranteed to work out of the box. To use Puppeteer with a different version of Chrome or Chromium, pass in the executable's path when creating a Browser instance: const browser = await puppeteer.

Does puppeteer work with Chrome?

Puppeteer is a Node library which provides a high-level API to control headless Chrome or Chromium over the DevTools Protocol. It can also be configured to use full (non-headless) Chrome or Chromium.


2 Answers

This full script should work, please not this doesnt handle if you are logged in already or if your account uses 2-factor authentication, good luck

const puppeteer = require('puppeteer');
(async () => {
  const browser = await puppeteer.launch({ headless: false})
  const page = await browser.newPage()

  const navigationPromise = page.waitForNavigation()

  await page.goto('https://accounts.google.com/')

  await navigationPromise

  await page.waitForSelector('input[type="email"]')
  await page.click('input[type="email"]')

  await navigationPromise

  //TODO : change to your email 
  await page.type('input[type="email"]', '[email protected]')

  await page.waitForSelector('#identifierNext')
  await page.click('#identifierNext')

  await page.waitFor(500);

  await page.waitForSelector('input[type="password"]')
  await page.click('input[type="email"]')
  await page.waitFor(500);

  //TODO : change to your password
  await page.type('input[type="password"]', 'yourpassword')

  await page.waitForSelector('#passwordNext')
  await page.click('#passwordNext')

  await navigationPromise

  //await browser.close()
})()
like image 58
Thibault Avatar answered Oct 02 '22 14:10

Thibault


This works for now... you can login once it loads like this. Get's around fedex login restrictions too :) Thanks @ABDELJALILAITETALEB !!!

const puppeteer = require('puppeteer-extra');
const StealthPlugin = require('puppeteer-extra-plugin-stealth');
puppeteer.use(StealthPlugin());
doit = async () => {
    const browser = await puppeteer.launch({args: ['--no-sandbox'], headless: false});
    let page = await browser.newPage();
    await page.goto('https://accounts.google.com');
}
doit();
like image 28
user433342 Avatar answered Oct 02 '22 12:10

user433342