如何使用Puppeteer选择具有相同类的所有子div? [英] How to select all child div with same class using Puppeteer?
问题描述
我是 Puppeteer 的新手,我正在尝试从使用相同类的两个 div 中获取 textContent.
<div class="post-item-info"><span class="post-item-status post-comment"></span>3<div class="post-item-info"><span class="post-item-status post-vote"></span>5
我期待的结果是返回一个数组 [3,5].我当前的代码如下.
let postInfo = element.querySelector('.post-item-info');
问题是它只返回第一个.请告诉我该怎么做.
你的选择器应该像 const nodes = element.querySelectorAll('.post-item-info');
.然后要访问返回集合中的单个项目,请使用传统的 for 循环,如
for(let i = 0; i
I'm new for Puppeteer and I'm trying to get textContent from two divs that using same class.
<div class="post-item">
<div class="post-item-info">
<span class="post-item-status post-comment"></span>
3
</div>
<div class="post-item-info">
<span class="post-item-status post-vote"></span>
5
</div>
</div>
The result that I'm expecting is return an array [3,5]. My current code is below.
let postInfo = element.querySelector('.post-item-info');
The problem is it is only return the first one. Please let me know how to do it.
Your selector should be like const nodes = element.querySelectorAll('.post-item-info');
. Then to access individual items in the returned collection, use a traditional for loop like
for(let i = 0; i < nodes.length; i++){
const currentNode = nodes[i];
// doStuffWith(currentNode);
}
这篇关于如何使用Puppeteer选择具有相同类的所有子div?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!