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

use*_*363 2 javascript node.js headless-browser google-chrome-devtools puppeteer

有什么样document.ready()Puppeteer

有:

page.waitForSelector(selector)
Run Code Online (Sandbox Code Playgroud)

由于任何HTML页面上都有很多同名选择器,因此如何确保该功能已加载正确的页面?这是一个简单的函数,但是在我以前使用它时引起了很多错误page.content()。我不确定该功能缺少什么。

Gra*_*ler 5

您可以使用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.
Run Code Online (Sandbox Code Playgroud)

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

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'});
Run Code Online (Sandbox Code Playgroud)