问题
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<mpl=default<mplcache=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('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()));
})
回答1:
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()
})()
回答2:
If a page load happens then the subsequent commands/scripts won't work as they aren't loaded properly. For that there's the waitForNavigation method.
Your code will look something like:
const elementHandle = await page.$('input');
await elementHandle.type('dp-conference@digitalpulp.com');
await page.click("#identifierNext");
// Wait for next pageload
await page.waitForNavigation(waitOptions)
const pw = await page.$('input[type="password"]');
来源:https://stackoverflow.com/questions/48572276/how-do-i-sign-into-google-using-puppeteer