admin管理员组文章数量:1315966
The HTML I am trying to parse with Puppeteer looks something like this:
<ul>
<li class="title"> item 1 </li>
<li class="title hide"> item 1 </li>
</ul>
And I am accessing the li
elements like this:
await page.$$eval("ul > li.title", nodes =>
nodes.map(element => {
return {
//some attributes
};
})
);
The oute extended is to only retrieve elements without class=hide
. Unfortunately hide
is a class that's in addition to title
, which is shared by all <li>
elements.
How can I refactor the Puppeteer code to exclude elements with hide
class?
The HTML I am trying to parse with Puppeteer looks something like this:
<ul>
<li class="title"> item 1 </li>
<li class="title hide"> item 1 </li>
</ul>
And I am accessing the li
elements like this:
await page.$$eval("ul > li.title", nodes =>
nodes.map(element => {
return {
//some attributes
};
})
);
The oute extended is to only retrieve elements without class=hide
. Unfortunately hide
is a class that's in addition to title
, which is shared by all <li>
elements.
How can I refactor the Puppeteer code to exclude elements with hide
class?
2 Answers
Reset to default 5:not(.hide)
You should use the :not()
CSS pseudo-class to select elements that do not include the class .hide
:
await page.$$eval('ul > li.title:not(.hide)', nodes =>
nodes.map(element => {
return {
// some attributes
};
})
);
.filter(e => !e.matches('.hide'))
On the other hand, you can also filter()
your nodes
to only include the elements that are not matches()
of the .hide
selector string:
await page.$$eval('ul > li.title', nodes =>
nodes.filter(e => !e.matches('.hide')).map(element => {
return {
// some attributes
};
})
);
Just add :not(.hide)
to your selector string:
page.$$eval("ul > li.title:not(.hide)", nodes =>
本文标签: javascriptExcluding elements with certain classes in PuppeteerStack Overflow
版权声明:本文标题:javascript - Excluding elements with certain classes in Puppeteer - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741995534a2409965.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论