Puppeteer - counting elements in the DOM

ε祈祈猫儿з 提交于 2020-05-28 12:01:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!