大多数 Puppeteer 问题都集中在如何确保页面已完全加载和呈现。我的问题恰恰相反:
使用 Puppeteer,如何尽快获得 javascript 访问权限,而不必等待 load
或 domcontentloaded
事件?
如果我不想/不需要等待整个页面呈现,什么时候尽早运行 page.evaluate
是安全的?
最佳答案
您正在寻找page.evaluateOnNewDocument
功能。来自文档:
The function is invoked after the document was created but before any of its scripts were run. This is useful to amend the JavaScript environment, e.g. to seed
Math.random
.
请注意,您必须在 page.goto
之前调用 page.evaluateOnNewDocument
。
关于javascript - 如何在 Puppeteer 中尽快获取文档响应并运行 javascript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55784366/