javascript - Getting a list of exclusive class name with a class name in puppeteer
问题描述
I am trying to get class names and check if at least one of the tags have red
class or not. So, if at least one of them includes red
class, the function must return true
, otherwise, false
.
The closest one is this:
const nodeList = await page.evaluate(() => {
const arrynodeList = document.querySelectorAll('.an_panel_list')
return arrynodeList
})
console.log('nodeList:', nodeList)
And I get
nodeList: { '0': {}, '1': {} }
For instance, the html looks like.
<div class="an_panel_list red">
<div class="an_panel_list">
<div class="an_panel_list">
<div class="an_panel_list">
And I get true
.
解决方案
I would try to solve everything in the evaluate
function:
const nodeList = await page.evaluate(() => {
const arrynodeList = document.querySelectorAll('.an_panel_list');
const redList = Array.prototype.slice.call(arrynodeList).filter(e => e.classList.contains("red"))
return {
divs: arrynodeList.length,
reds: redList.length
}
})
console.log(nodeList)
推荐阅读
- c - 从文件中读取矩阵并检索它们的维度
- android - 当我使用 minifyEnabled 应用程序时不断崩溃
- python-3.x - 决策树模型的精度为零
- javascript - 为什么 db.findAll() 不是函数?
- java - libGDX:根据用户输入更改 PNG 的颜色
- c++ - 移动 QT 项目,重命名文件夹和项目名称,Moc'ing 返回“'-I' 后缺少值”
- c# - 当属性网格的属性值更改时防止对象修改
- html - 如何有效地在画布上加载和绘制表情符号?
- java - 如何使用 k8s-java-client 在同一个 k8s 集群上的同一服务的 Pod 之间进行通信?
- react-router - 如何使 createObjectUrl() 与 react-router 一起工作?