简体   繁体   English

如何在Puppeteer中获取文档响应并尽快运行javascript?

[英]How to get the document response and run javascript as soon as possible in Puppeteer?

Most Puppeteer questions focus around how to ensure the page has fully loaded and rendered. 大多数Puppeteer问题集中在如何确保页面已完全加载和呈现上。 My question is the opposite: 我的问题是相反的:

Using Puppeteer, how can I get javascript access as soon as possible without having to wait for the load or domcontentloaded events? 使用Puppeteer,如何不必等待loaddomcontentloaded事件而尽快获得javascript访问?

When is it safe to run page.evaluate at the earliest opportunity, if I don't want/need to wait for the full page to render? 如果我不想/不需要等待整个页面呈现,那么尽早运行page.evaluate何时安全?

You are looking for the page.evaluateOnNewDocument function. 您正在寻找page.evaluateOnNewDocument函数。 From the docs: 从文档:

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, eg to seed Math.random . 这对于修改JavaScript环境很有用,例如,可以Math.random

Be aware that you have to call page.evaluateOnNewDocument before page.goto . 请注意,您必须在page.evaluateOnNewDocument之前调用page.goto

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM