Puppeteer - Click on tag behind text

Viewed 34

I am quite new to puppeteer and am stuck with trying to click on an element. In the image, that is the "i" element, I want to click on.

I want to click on the  element, but there is the nb space before it

When I try to click it, I get the error "Node is either not visible or not an HTMLElement". I guess it is not visible, because there it the nb space in front of it.

Is there a way still to click on it? When I manually click on it, it works, so I would think that puppeteer would also be able to do it?

Thanks, Benni

1 Answers

After quite some testing I found a way how to click it. First, this didnt work:

page.click(selector)

This brought up the error that specified before.

What worked now was that:

page.evaluate(function (selector) {

  const elements = document.querySelectorAll(selector);    
  elements.forEach((element) => {
    element.click();
  });

}, selector);

Not sure why the element is coming up multiple times. It should be there only once.

I am not an expert with puppeteer, but happy that this worked. Maybe it helps someone else.

Related