【问题标题】:How to fix Node.js puppeteer keyboard.type() causes TypeError如何修复 Node.js puppeteer keyboard.type() 导致 TypeError
【发布时间】:2021-07-02 04:15:40
【问题描述】:

我有一些代码正在填写表格。它能够找到我需要的所有部分,但无法输入其中之一。

一开始,我只是等待输入字段加载,然后写信给他们page.type()。用户名工作正常,但密码失败。 我将page.type() 拆分为page.click() 然后page.keyboard.type() 以隔离问题。

我正在使用 typescript 编写并使用 ts-node 运行文件

两次运行都出现了同样的错误:

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"]');

这段代码在我的另一个项目中确实有效,是什么导致了这个错误?

【问题讨论】:

  • 是user.password里面的字符串吗?
  • @Md.AbuTaher 是的
  • 也许你没有传递字符串,而是 int 或 object。你能仔细检查一下吗?
  • @Md.AbuTaher 就是这样,我忘记定义密码值了。

标签: node.js puppeteer


【解决方案1】:

发生错误是因为user.password 不是字符串。是undefined

【讨论】:

  • 谢谢。我一直在搜索问题,发现undefined是根本原因
【解决方案2】:

就我而言,我传递的是 Number 而不是 String。 Number 确实不可迭代,应该通过 String

【讨论】:

    【解决方案3】:

    您可以使用 JSON.stringify

    将数字转换为字符串
    await page.keyboard.type(JSON.stringify(user.password));
    

    【讨论】:

      猜你喜欢
      • 2021-09-14
      • 2021-07-10
      • 2019-02-09
      • 2019-09-18
      • 1970-01-01
      • 2021-08-07
      • 2012-09-22
      • 1970-01-01
      • 2019-10-23
      相关资源
      最近更新 更多