2

我正在尝试将 puppeteer 与本地脚本文件一起使用。

当我托管文件并将addScriptTag()与本地主机地址一起使用时,我会加载脚本文件。这并不理想。我需要直接从路径中使用本地文件。在这种情况下,当前工作目录是 /maps。我在 addScriptTag() 函数的选项中将相对路径设置为路径,但在这个阶段代码对我来说只是黑暗。没有错误,也没有踏入任何事情。

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();
})();

欢迎任何帮助,因为我发现 puppetetteer 文档越来越令人沮丧,并且到目前为止还没有(m)任何工作示例。

4

1 回答 1

3

是版本问题。此方法仅在昨天的最新更新 0.12 中引入。我前一天安装​​了 puppeteer。呸!

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

于 2017-10-16T16:39:27.013 回答