Why I got document is not defined error in puppeteer?

13,977

Solution 1

I think document would only be available within page.evaluate (according to puppeteer documentation )

Try:

async function gallery(page) {
   await page.waitFor(3000);
   await page.evaluate(() => {
      document.querySelector('div.image').click();
   })
}

Solution 2

you are calling invalid element , you can check this document

await page.evaluate(() => {
  document.querySelector('div.image').click();
});
Share:
13,977
Con Troll
Author by

Con Troll

Updated on June 20, 2022

Comments

  • Con Troll
    Con Troll almost 2 years

    I'd like to simulate a click on a gallery (<div class="image">) but when I try to run this code, I got document not defined error.

    async function gallery(page) {
     await page.waitFor(3000);
     await page.click(document.querySelector('.div image'));
    }
    

    What's the problem here? How can I use document.querySelector correctly with puppeteer?

  • Con Troll
    Con Troll about 5 years
    I still got document is not defined with div.image too.
  • Muhammad Ali
    Muhammad Ali about 5 years
    you can check this one
  • Vipin Verma
    Vipin Verma almost 3 years
    How do you use it while debugging in console? It seems to be working fine, but when in debug mode with a breakout within page.eval it gives doc is not defined error