我正在尝试获取类名并检查是否至少有一个标签具有 red
类。因此,如果其中至少一个包含 red
类,则该函数必须返回 true
,否则返回 false
。
最接近的是这个:
const nodeList = await page.evaluate(() => {
const arrynodeList = document.querySelectorAll('.an_panel_list')
return arrynodeList
})
console.log('nodeList:', nodeList)
我明白了
nodeList: { '0': {}, '1': {} }
例如,html 看起来像。
<div class="an_panel_list red">
<div class="an_panel_list">
<div class="an_panel_list">
<div class="an_panel_list">
我得到 true
。
最佳答案
我会尝试在 evaluate
函数中解决所有问题:
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)
关于javascript - 在 puppeteer 中获取带有类名的独占类名列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54791993/