如何使用Puppeteer登录Google? [英] How do I sign into Google using Puppeteer?

查看:146
本文介绍了如何使用Puppeteer登录Google?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Puppeteer,并且正在尝试登录我的Gmail帐户

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

URL:目前,我的代码输入电子邮件表单,然后提交回车,然后,当页面转到密码屏幕时,无法在密码输入中写入内容.这可能是因为从技术上讲它不是一个新页面,而是相同的页面.无论哪种方式,当我在电子邮件页面上按Enter时,似乎都无法与该新页面进行交互.

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('dp-conference@digitalpulp.com');
  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()));

})

推荐答案

此完整脚本应该可以正常工作,如果您已经登录或者您的帐户使用了2要素身份验证,那么请祝您好运

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"]', 'youremail@gmail.com')

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

这篇关于如何使用Puppeteer登录Google?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆