10

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

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?

Grant Miller
  • 27,532
  • 16
  • 147
  • 165
user938363
  • 9,990
  • 38
  • 137
  • 303

1 Answers1

20

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

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

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();
Sam R.
  • 16,027
  • 12
  • 69
  • 122
Grant Miller
  • 27,532
  • 16
  • 147
  • 165
  • 1
    This ist not possible with headless: true I guess? I'm running puppeteer in a lambda function so it is always headless. Any idea how to solve it? – Tobi Jun 14 '19 at 05:05
  • 1
    Doesn't make sense. How could you wait for user input when a user can't view a headless browser? @Tobi – FabricioG Mar 02 '20 at 20:56
  • 1
    He probably wants to ask a remote user for an input and them pass it to puppetteer – motobói Sep 22 '20 at 15:52
  • Then he can just await any promise outside of puppeteer – Aidan May 01 '21 at 10:36
  • I want to know what would happen when we deploy this thing to cloud servers, we don't have GUI there, how would we able to replicate these steps ? – Muhammad Uzair Jul 20 '22 at 11:25