Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix Node.js puppeteer keyboard.type() causes TypeError

I have some code that is filling out a form. It is able to find all the parts I need but it failes to type in one of them.

At first, I started by just waiting for the input fields to load, then wrote to them with page.type(). The username worked fine but the password failed. I split page.type() to page.click() and then page.keyboard.type() in order to isolate the problem.

I'm writing with typescript and running the files with ts-node

The same error was raised on both runs:

TypeError: text is not iterable
    at Keyboard.type (...\node_modules\puppeteer\lib\Input.js:162:24)
    at Keyboard.<anonymous> (...\node_modules\puppeteer\lib\helper.js:112:23)
    at ...\src\backend\index.ts:67:37
    at step (...\src\backend\index.ts:33:23)
    at Object.next (...\src\backend\index.ts:14:53)        
    at fulfilled (...\src\backend\index.ts:5:58)
await page.waitFor('input[type="text"]')    // Makes sure the form was loaded
    await page.type('input[type="text"]', user.username);
    await page.waitFor('input[type="password"]')    // Makes sure the form was loaded
    try{
        // await page.type('input[type="password"]', user.password);
        await page.click('input[type="password"]');    // Click password field
        await page.waitFor(5000)
        await page.keyboard.type(user.password);
    }
    catch(e){
        console.error(e)
    }
    await page.waitFor('input[type="submit"]')    // Makes sure the form was loaded
    await page.click('input[type="submit"]');

This code definitely worked in another project I had, what can be causing this error?

like image 888
Koby 27 Avatar asked Sep 08 '19 21:09

Koby 27


3 Answers

The error occurred because user.password was not a string. It was undefined.

like image 50
Koby 27 Avatar answered Oct 08 '22 08:10

Koby 27


In my case, I was passing a Number instead of a String. Number is not iterable indeed, should pass a String.

like image 31
Alison Moura Avatar answered Oct 08 '22 08:10

Alison Moura


You can convert number to string using JSON.stringify

await page.keyboard.type(JSON.stringify(user.password));
like image 3
object.vz Avatar answered Oct 08 '22 09:10

object.vz