简体   繁体   English

为什么我在puppeteer中得到文件未定义错误?

[英]Why I got document is not defined error in puppeteer?

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. 我想模拟在图库( <div class="image"> )上的单击,但是当我尝试运行此代码时,出现文档未定义错误。

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? 我该如何正确地使用puppeteer的document.querySelector?

you are calling invalid element , you can check this document 您正在调用无效元素,可以检查此文档

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

I think document would only be available within page.evaluate (according to puppeteer documentation ) 我认为文档只能在page.evaluate (根据page.evaluate 文档

Try: 尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM