gpt4 book ai didi

javascript - 类似于 Puppeteer 中的 document.ready() 的功能?

转载 作者:搜寻专家 更新时间:2023-11-01 00:19:43 25 4
gpt4 key购买 nike

Puppeteer 中是否有类似document.ready() 的东西?

有:

page.waitForSelector(selector)

既然在任何HTML页面上都有那么多同名选择器,那么这个函数怎么能确定加载了正确的页面呢?这是一个简单的函数,但是当我在 page.content() 之前使用它时导致了很多错误。我不确定这个函数遗漏了什么。

最佳答案

您可以使用 page.waitForNavigation()作为 jQuery 的 document.ready() 的替代品功能:

await page.waitForNavigation({waitUntil: 'load'});             // consider navigation to be finished when the load event is fired.
await page.waitForNavigation({waitUntil: 'domcontentloaded'}); // consider navigation to be finished when the DOMContentLoaded event is fired.
await page.waitForNavigation({waitUntil: 'networkidle0'}); // consider navigation to be finished when there are no more than 0 network connections for at least 500 ms.
await page.waitForNavigation({waitUntil: 'networkidle2'}); // consider navigation to be finished when there are no more than 2 network connections for at least 500 ms.

或者,您可以使用 page.goto() 中的 waitUntil 选项等待文档加载:

await page.goto('https://example.com/', {waitUntil: 'load'});
await page.goto('https://example.com/', {waitUntil: 'domcontentloaded'});
await page.goto('https://example.com/', {waitUntil: 'networkidle0'});
await page.goto('https://example.com/', {waitUntil: 'networkidle2'});

关于javascript - 类似于 Puppeteer 中的 document.ready() 的功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53017788/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com