如何暂停并等待用户使用Puppeteer输入? [英] How can I pause and wait for user input with Puppeteer?

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

问题描述

我需要暂停 Puppeteer 并等待用户输入用户名 password ,然后继续。这是一个 nodejs 8.12.0应用程序。

I need to make Puppeteer pause and wait for user input of username and password before continuing. It is a nodejs 8.12.0 app.

(async () => {
    const browser = await puppeteer.launch({headless: false});
    const page = await browser.newPage();   
    await page.goto('https://www.myweb.com/login/');

    //code to wait for user to enter username and password, and click `login`

    const first_page = await page.content();

   //do something 

    await browser.close();
)}();

基本上该程序已暂停并等待用户单击登录按钮。是否可以在 Puppeteer 中执行此操作?还是我还能做什么?

Basically the program is halted and waits until the user clicks the login button. Is it possible to do this in Puppeteer? Or what else can I do?

推荐答案

您可以使用 page.waitForFunction() 等待

You can use page.waitForFunction() to wait for a function to return a truthy value before continuing.

以下函数将一直等待,直到 #username #password 字段在继续操作之前包含一个值:

The following function will wait until the #username and #password fields contain a value before continuing:

await page.waitForFunction(() => {
  const username = document.getElementById('username').value;
  const password = document.getElementById('password').value;
  
  return username.length !== 0 && password.length !== 0;
});

但是由于您要输入 #username #password 自己,您可以简单地使用:

But since you are entering the #username and #password yourself, you can simply use:

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

这篇关于如何暂停并等待用户使用Puppeteer输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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