admin管理员组

文章数量:1336631

I am trying to use puppeteer with a local script file.

I get the script file to load when I host the file and use addScriptTag() with the localhost address. This is not ideal. I need to use the local file directly from the path. The current working directory is /maps in this case. I set the relative path as path in the options of the addScriptTag() function but the code just goes dark on me at this stage. There is no error and no stepping into anything.

console.log(`Current directory: ${process.cwd()}`);
// C:\Users\dbauszus\Documents\GitHub\maps
(async () => {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.setContent(jsr.templates('./views/report.html').render(), {waitUntil: 'load'});
    // works with an url to the same file.
    // await page.addScriptTag('http://localhost:3000/maps/js/build/report_bundle.js');
    // path for js file on windows C:\Users\dbauszus\Documents\GitHub\maps\public\js\build\report_bundle.js
    await page.addScriptTag({path: 'public\\js\\build\\report_bundle.js'});
    await page.screenshot({path: 'example.png'});
    await browser.close();
})();

Any help will be wele as I find the puppetteer documentation increasingly frustrating and there aren't (m)any working examples as of now.

I am trying to use puppeteer with a local script file.

I get the script file to load when I host the file and use addScriptTag() with the localhost address. This is not ideal. I need to use the local file directly from the path. The current working directory is /maps in this case. I set the relative path as path in the options of the addScriptTag() function but the code just goes dark on me at this stage. There is no error and no stepping into anything.

console.log(`Current directory: ${process.cwd()}`);
// C:\Users\dbauszus\Documents\GitHub\maps
(async () => {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.setContent(jsr.templates('./views/report.html').render(), {waitUntil: 'load'});
    // works with an url to the same file.
    // await page.addScriptTag('http://localhost:3000/maps/js/build/report_bundle.js');
    // path for js file on windows C:\Users\dbauszus\Documents\GitHub\maps\public\js\build\report_bundle.js
    await page.addScriptTag({path: 'public\\js\\build\\report_bundle.js'});
    await page.screenshot({path: 'example.png'});
    await browser.close();
})();

Any help will be wele as I find the puppetteer documentation increasingly frustrating and there aren't (m)any working examples as of now.

Share Improve this question asked Oct 16, 2017 at 15:56 Dennis BauszusDennis Bauszus 1,8024 gold badges21 silver badges50 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

It's a version problem. This method was only introduced in the latest update 0.12 from yesterday. I installed puppeteer the day before. Meh!

https://github./GoogleChrome/puppeteer/issues/949

本文标签: javascriptHow to use addScriptTag() with local file path in PuppeteerStack Overflow