Puppeteer in NodeJS reports ‘Error: Node is either not visible or not an HTMLElement’

If your code looks like this

const button = await page.$(selector);
await button.click();

Try changing

await button.click();

to

await button.evaluate(b => b.click());

The difference is that button.click() clicks using Puppeteer’s ElementHandle.click() which

  1. scrolls the page until the element is in view
  2. gets the bounding box of the element (this step is where the error happens) and finds the screen x and y pixel coordinates of the middle of that box
  3. moves the virtual mouse to those coordinates and sets the mouse to “down” then back to “up”, which triggers a click event on the element under the mouse

whereas button.evaluate(b => b.click()) “clicks” the element by running the JavaScript HTMLElement.click() method on the given element in the browser context, which fires a click event. It doesn’t scroll the page or move the mouse and works even if the element is off-screen.

Leave a Comment