最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I pause and wait for user input with Puppeteer? - Stack Overflow

programmeradmin3浏览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('/');

    //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?

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?

Share Improve this question edited Oct 25, 2018 at 23:43 Grant Miller 29k16 gold badges155 silver badges168 bronze badges asked Oct 25, 2018 at 21:57 user938363user938363 10.4k41 gold badges159 silver badges326 bronze badges 3
  • you will be the one to trigger that action right? so why do you want to wait indefinitely for an action to happen? – karthick Commented Oct 25, 2018 at 22:48
  • Hi, the user will enter login information and the code not continue without a valid login. – user938363 Commented Oct 26, 2018 at 3:10
  • Depends on the page. Probably the best way is to wait for a selector on the subsequent page after login, or wait for that URL to be active. Set a timeout for however long you want to give the user to log in. – ggorlen Commented May 30, 2024 at 4:48
Add a comment  | 

1 Answer 1

Reset to default 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();
发布评论

评论列表(0)

  1. 暂无评论