javascript 如何登录Puppeteer?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50074799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 08:53:59  来源:igfitidea点击:

How to login in Puppeteer?

javascriptnode.jsgoogle-chrome-devtoolspuppeteerheadless

提问by Joonho Lee

I'm new to JavaScript and Puppeteer. I tried the login code below, but it failed. In comparison, I added page2and succeeded. How can I solve it?

我是 JavaScript 和 Puppeteer 的新手。我尝试了下面的登录代码,但失败了。相比之下,我添加page2并成功了。我该如何解决?

const CREDS = require('./creds');

async function main() {
  const puppeteer = require('puppeteer');
  const browser = await puppeteer.launch({headless: false});

  const page = await browser.newPage();
  await page.setViewport({width: 1200, height: 720})
  await page.goto('https://www.daum.net');
  await page.waitForNavigation();
  await page.type('#id', CREDS.username);
  await page.type('#loginPw', CREDS.password);
  await page.click('#loginSubmit');

  const page2 = await browser.newPage();
  await page2.setViewport({width: 1200, height: 720})
  await page2.goto('https://google.com');
  await page2.type('#lst-ib', 'Headless Chrome');
}

main();

回答by Md. Abu Taher

page.waitForNavigation();waits for navigation after a clickor any navigation action that triggers from the page.you should probably add the waitForNavigation after the page.click.

page.waitForNavigation();click从页面触发的一个或任何导航操作之后等待导航。您可能应该在页面之后添加 waitForNavigation。单击。

await Promise.all([
      page.click('#loginSubmit'),
      page.waitForNavigation({ waitUntil: 'networkidle0' }),
]);

It will wait until both promises resolves.

它将等到两个承诺都解决。

So now your initial code would look like this,

所以现在你的初始代码看起来像这样,

const puppeteer = require('puppeteer');
async function main() {
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
await page.setViewport({width: 1200, height: 720})
await page.goto('https://www.daum.net', { waitUntil: 'networkidle0' }); // wait until page load
await page.type('#id', CREDS.username);
await page.type('#loginPw', CREDS.password);
// click and wait for navigation
await Promise.all([
          page.click('#loginSubmit'),
          page.waitForNavigation({ waitUntil: 'networkidle0' }),
]);
}

main();

Note: Answer aside, I cannot test this since I don't have a login for daum.netand I cannot see the actual error you are facing. If you can try the solution provided above, and share the results, it'd be much more helpful.

注意:除了回答之外,我无法对此进行测试,因为我没有登录名,daum.net也看不到您面临的实际错误。如果您可以尝试上面提供的解决方案并分享结果,那将会更有帮助。

回答by Grant Miller

page.waitForNavigation()

page.waitForNavigation()

Logging in to a website using Puppeteer is generally as simple as using the following code:

使用 Puppeteer 登录网站通常与使用以下代码一样简单:

await page.goto('https://www.example.com/login');

await page.type('#username', 'username');
await page.type('#password', 'password');

await page.click('#submit');

await page.waitForNavigation(); // <------------------------- Wait for Navigation

console.log('New Page URL:', page.url());

Note:Remember to use page.waitForNavigation()after clicking the submit button to wait for the home page to display before moving forward.

注意:记得page.waitForNavigation()点击提交按钮后使用,等待首页显示后再前进。