我面临的问题是 Puppeteer 无法加载指定相对路径的资源(例如图像 src
或 css url()< 中的
)。
然后我使用 background.png
/setContent()
加载本地文件的内容,结果与我做同样的事情非常不同,但使用 goto(file://)
.
显然,然后使用'setContent()' 页面停留在'about:page' 以防止具有相对路径的资源加载。请问有没有什么办法可以指定文档位置并从字符串中加载内容?
我会使用 goto
但我需要先使用 Handlebar 预处理 html。
现在,我执行以下操作:
await page.goto(framePath);
let content = await page.content();
//Preprocess content here
await page.setContent(processedContent);
但感觉不对,我想知道这是否是更好的解决方案。
最佳答案
setContent
内部无非就是这个函数调用:
await this.evaluate(html => {
document.open();
document.write(html);
document.close();
}, html);
基本上,它在已经加载的文档上写入 HTML。使用 goto 给出一些位置一点也不差。您可以创建一个 empty.html
页面,并将其用作您自己定位的空白 Canvas 。
关于javascript - 如果使用 setContent 加载页面,Puppeteer 将不会加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62592345/