木偶-计数DOM中的元素 [英] Puppeteer - counting elements in the DOM
本文介绍了木偶-计数DOM中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道这里已经回答了
但是,按照这种方法,我得到的结果为0
Yet, following this approach, I get 0 as my result
使用page.$$
-在我的测试中,我总是得到0
using page.$$
- in my test, I always get 0
console.log((await page.$$('.clients-table > tbody > tr > td')).length);
在浏览器中使用document.querySelectorAll()
进行了验证,结果为4
verified in browser using document.querySelectorAll()
and the result is 4
怎么可能呢?
推荐答案
数据尚未加载.您可以使用waitForSelector
来确保正确加载数据.
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);
还可以使用waitUntil: 'networkidle0'
参数来确保页面已完全加载.
Also you can make sure the page is fully loaded using waitUntil: 'networkidle0'
argument.
这篇关于木偶-计数DOM中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文