最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Excluding elements with certain classes in Puppeteer - Stack Overflow

programmeradmin2浏览0评论

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?

Share Improve this question edited Nov 20, 2018 at 5:32 Grant Miller 29.1k16 gold badges155 silver badges168 bronze badges asked Nov 19, 2018 at 21:41 neo-technokerneo-technoker 3892 gold badges11 silver badges27 bronze badges
Add a ment  | 

2 Answers 2

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 =>
发布评论

评论列表(0)

  1. 暂无评论