Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excluding elements with certain classes in Puppeteer

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 outcome 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?

like image 764
Theepan Thevathasasn Avatar asked Feb 04 '26 23:02

Theepan Thevathasasn


2 Answers

: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
    };
  })
);
like image 195
Grant Miller Avatar answered Feb 06 '26 12:02

Grant Miller


Just add :not(.hide) to your selector string:

page.$$eval("ul > li.title:not(.hide)", nodes =>
like image 28
CertainPerformance Avatar answered Feb 06 '26 12:02

CertainPerformance