I am trying to pass the util module object to puppeteer page.evaluate
with no success. I understand this question was asked in How to pass required module object to puppeteer page.evaluate
but the solution provided does not work in my case. 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);
})();
Shows error:
UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Evaluation failed: ReferenceError: util is not defined
Thank you
Edit 1
Adding const util = require("util");
in the initial lines and doesn't work as shown in How to pass required module object to puppeteer page.evaluate.
Edit 2
Even when I use browserify I can't seem to inject the util
module into the puppeteer page. Steps:
On project PATH
, create main.js
as follows:
var util = require('util');
Then on PATH
in terminal: browserify main.js -o bundle.js
. A file bundle.js
appears in the project PATH
.
Then run the following:
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);
})();
Error:
UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Evaluation failed: TypeError: Cannot read property 'format' of undefined at :5:55