英文:
Puppeteer - How to get an array of elements of a first element?
问题
我知道$$eval
获取一个数组,$eval
获取遇到的第一个元素。
但我有以下问题/疑问:如何从Puppeteer找到的第一个元素中获取一个数组?我需要一个包含第一个figure标签内div内容的数组。
<div>
<figure class="test">
<div></div>
<div></div>
</figure>
<p></p>
<figure class="test">
<div></div>
</figure>
</div>
我该如何解决这个问题?
英文:
I know that $$eval
gets an array and $eval
gets the first element that encounters.
But I have the following problem/question: how can I get an array from the first element that Puppeteer finds? I need an array with the content of div of the first figure tag.
<div>
<figure class="test">
<div></div>
<div></div>
</figure>
<p></p>
<figure class="test">
<div></div>
</figure>
</div>
How can I tackle this?
答案1
得分: 0
以下是翻译好的部分:
For accessing the first child div:
const requiredEl = await page.$eval('figure', _figure => _figure.firstChild);
For accessing all children div:
const requiredEls = await page.$eval('figure', _figure => _figure.children);
英文:
Well that should be super easy.
For accessing the first child div:
const requiredEl = await page.$eval('figure', _figure => _figure.firstChild);
<br>
For accessing all children div:
const requiredEls = await page.$eval('figure', _figure => _figure.children);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论