javascript - How to pass required module object to puppeteer page.evaluate - Stack Overflow

Puppeteer version: 1.0.0PlatformOS version: Windows 10Node.js version: 8.9.3Here is my code:const p

  • Puppeteer version: 1.0.0
  • Platform / OS version: Windows 10
  • Node.js version: 8.9.3

Here is my code:

const puppeteer = require('puppeteer');
const varname = require('varname');

...

const page = await browser.newPage();
await page.goto(url);
let generalInfo = await page.evaluate(() => {
        let elements = Array.from(document.querySelectorAll('#order-details > table > tbody > tr'));
        let res = {};
        elements.map((tr) => {
            let split = tr.innerText.trim().split('\t');
            res[varname.camelback(split[0])] = split[1]; // Here is: ... Error: Evaluation failed: ReferenceError: varname is not defined
        });
        return res;
    }); 

...

await browser.close();

Shows error:

UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Evaluation failed: ReferenceError: varname is not defined

  • Puppeteer version: 1.0.0
  • Platform / OS version: Windows 10
  • Node.js version: 8.9.3

Here is my code:

const puppeteer = require('puppeteer');
const varname = require('varname');

...

const page = await browser.newPage();
await page.goto(url);
let generalInfo = await page.evaluate(() => {
        let elements = Array.from(document.querySelectorAll('#order-details > table > tbody > tr'));
        let res = {};
        elements.map((tr) => {
            let split = tr.innerText.trim().split('\t');
            res[varname.camelback(split[0])] = split[1]; // Here is: ... Error: Evaluation failed: ReferenceError: varname is not defined
        });
        return res;
    }); 

...

await browser.close();

Shows error:

UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Evaluation failed: ReferenceError: varname is not defined

Share Improve this question edited Feb 16, 2018 at 16:30 Alex K asked Feb 15, 2018 at 20:18 Alex KAlex K 3,2411 gold badge25 silver badges32 bronze badges 1
  • This SO question is relevant: stackoverflow./questions/53676445/… – Motin Commented Aug 10, 2021 at 9:29
Add a ment  | 

1 Answer 1

Reset to default 14

You want to use module varname in a browser context. To achieve that you should use page.addScriptTag() to inject varname to browser like this:

const puppeteer = require('puppeteer');

(async () => {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();

    await page.addScriptTag({ path: './node_modules/varname/build/varname.js' });
    const generalInfo = await page.evaluate(() => {
        return window.varname.camelback('foo_bar_baz');
    });

    console.log(generalInfo); // prints fooBarBaz
    await browser.close();
})();

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743634716a4481953.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信