为什么我在 puppeteer 中收到 document is not defined 错误?

Why I got document is not defined error in puppeteer?

我想模拟对图库的点击 (<div class="image">),但是当我尝试 运行 这段代码时,出现文档未定义错误。

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

这里有什么问题?如何正确使用 puppeteer document.querySelector?

你正在调用无效元素,你可以检查这个document

await page.evaluate(() => {
  document.querySelector('div.image').click();
});

我认为文档只能在 page.evaluate 内使用(根据 puppeteer documentation

尝试:

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