问题
I know this has been answered here
Puppeteer - counting elements by class name
Yet, following this approach, I get 0 as my result
using page.$$
- in my test, I always get 0
console.log((await page.$$('.clients-table > tbody > tr > td')).length);
verified in browser using document.querySelectorAll()
and the result is 4
How could this be???
回答1:
The data is not loaded yet. You can use waitForSelector
to ensure the data is loaded properly.
So just add this before counting the numbers,
await page.waitForSelector('.clients-table > tbody > tr > td');
console.log((await page.$$('.clients-table > tbody > tr > td')).length);
Also you can make sure the page is fully loaded using waitUntil: 'networkidle0'
argument.
来源:https://stackoverflow.com/questions/55168344/puppeteer-counting-elements-in-the-dom