提问者:小点点

错误:计算失败:ReferenceError:未定义util


我试图将util模块对象传递给puppeteerpage.evaluate,但没有成功。我知道这个问题是在如何将所需的模块对象传递给puppeteer page.evaluate中提出的,但提供的解决方案在我的情况下不起作用。MWE:

const puppeteer = require("puppeteer");

const path = "http://books.toscrape.com/";

// scrape funs 
(async () =>{
    const browser = await puppeteer.launch({headless: false});
    const page = await browser.newPage();
    await page.goto(path, {waitUntil: "networkidle2", timeout: 0});
    await page.waitFor(1000);
    await page.addScriptTag({path: './node_modules/util/util.js'});
    // selector with replaceable element
    const buttonText = await page.evaluate(() => {
        let selectorButton = "#default > div > div > div > div > section > div:nth-child(2) > ol > li:nth-child(%s) > article > div.product_price > form > button";
        let buttons = [];
        for(let i = 1; i < 21; i ++){
            let textOut = document.querySelector(util.format(selectorButton, i)).innerText;
            buttons.push(textOut);
        };
        return buttons;  
    });

// return
await browse.close();
console.log(buttonText);
})();

显示错误:

UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:1):错误:计算失败:ReferenceError:未定义util

谢谢

在初始行中添加const util=require(“util”);并不像如何将required module object传递给puppeteer page.evaluate中所示的那样起作用。

即使在使用browserify时,我似乎也无法将util模块注入到puppeteer页面中。步骤:

在项目path上,创建main.js,如下所示:var util=require('util');

然后在终端中的path上:browserify main.js-o bundle.js。文件bundle.js出现在项目path中。

然后运行以下操作:

const puppeteer = require("puppeteer");
const path = "http://books.toscrape.com/";

// scrape funs 
(async () =>{
    const browser = await puppeteer.launch({headless: false});
    const page = await browser.newPage();
    await page.goto(path, {waitUntil: "networkidle2", timeout: 0});
    await page.waitFor(1000);
    await page.addScriptTag({path: "main.js"});
    await page.addScriptTag({path: "bundle.js"});
    // selector with replaceable element
    const buttonText = await page.evaluate(() => {
        let buttons = [];
        let selectorButton = "#default > div > div > div > div > section > div:nth-child(2) > ol > li:nth-child(%s) > article > div.product_price > form > button";
        for(let i = 1; i < 21; i ++){
            let textOut = document.querySelector(util.format(selectorButton, i)).innerText;
            buttons.push(textOut);
        };
        return buttons;  
    });

// return
await browse.close();
console.log(buttonText);
})();

错误:

UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:1):错误:计算失败:TypeError:无法读取未定义的属性“Format”(位于:5:55)


共1个答案

匿名用户

您需要包含./node_modules/util/util.js的浏览器兼容构建。您可以使用Browserify来完成此操作,或者使用他们的在线服务Browserify wizard-util下载Browserify版本。

https://try-puppeteer.appspot.com/上的代码

const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto("http://books.toscrape.com/", {waitUntil: "networkidle2", timeout: 0});
await page.waitFor(1000);

//Copy of https://wzrd.in/standalone/util@latest
await page.addScriptTag({url: "https://cdn.rawgit.com/brahma-dev/099d0d6d43a5d013603bcd245ee7a862/raw/b0c6bb82905b5b868c287392000dc2487c41994d/util.js"});

// selector with replaceable element
const buttonText = await page.evaluate(() => {
    let buttons = [];
    let selectorButton = "#default > div > div > div > div > section > div:nth-child(2) > ol > li:nth-child(%s) > article > div.product_price > form > button";
    for(let i = 1; i < 21; i ++){
        let textOut = document.querySelector(util.format(selectorButton, i)).innerText;
        buttons.push(textOut);
    };
    return buttons;  
});

// return
await browser.close();
console.log(buttonText);