Puppeteer - TypeError: require(...) 不是函数

时间:2021-02-19 17:08:13

标签: javascript node.js puppeteer

我已经开始使用 puppeteer 来测试登录表单,我已经尝试了以下代码,但是当我尝试运行脚本时,我在 vs 终端中遇到了这个错误

index.js

*(...)
#!/usr/bin/env node

require('dotenv').config()
const puppeteer = require('puppeteer')

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://www.google.com');
  // other actions...
  //await browser.close();
})();

代码与 puppeteer 文档中提供的示例相同。如您所见,我正在尝试制作 cli 脚本。如果我尝试使用自定义代码,则会出现此错误

index.js

(async () => {
^

TypeError: require(...) is not a function
    at Object.<anonymous> (/Users/dev/Desktop/fuserbot/index.js:30:1)
    at Module._compile (node:internal/modules/cjs/loader:1108:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10)
    at Module.load (node:internal/modules/cjs/loader:973:32)
    at Function.Module._load (node:internal/modules/cjs/loader:813:14)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
    at node:internal/main/run_main_module:17:47
#!/usr/bin/env node

require('dotenv').config();
const puppeteer = require('puppeteer');

(async () => {
    //
    const browser = await puppeteer.launch();
    //
    const page = await browser.newPage();
    //
    page.setRequestInterception(true);
    //
    page.on('request', (request) => {
        console.log(request.url(), request.headers(), request.method(), request.postData());
    });
    //
    await page.goTo('https://www.examplesite.com');
    //
    await page.type('#email', process.env.MY_EMAIL, {delay: 2000});
    await page.type('#pass', process.env.MY_PASS, {delay: 2000});
    //
    await page.click('button[type="submit"]');
    //
    await page.waitForNavigation();

    console.log('New url', page.url());
})();

代码有什么问题?

0 个答案:

没有答案