admin管理员组文章数量:1208155
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?
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?
Share Improve this question edited Apr 16, 2019 at 7:25 Con Troll asked Apr 16, 2019 at 7:18 Con TrollCon Troll 1591 gold badge1 silver badge9 bronze badges 6 | Show 1 more comment2 Answers
Reset to default 12I 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();
})
}
you are calling invalid element , you can check this document
await page.evaluate(() => {
document.querySelector('div.image').click();
});
本文标签: javascriptWhy I got document is not defined error in puppeteerStack Overflow
版权声明:本文标题:javascript - Why I got document is not defined error in puppeteer? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738758375a2110809.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
const partnersOnPage = await page.evaluate(() => Array.from(document.querySelectorAll('div.listing__card a.listing__thumbnail')) .map(element => element.href.substring(element.href.lastIndexOf('/') + 1)) );
so there is document in nodejs and puppeteer. – Con Troll Commented Apr 16, 2019 at 7:28document
would only be available withinpage.avaluate
(according to github.com/GoogleChrome/puppeteer) Try:async function gallery(page) { await page.waitFor(3000); await page.evaluate(() => { document.querySelector('div.image').click(); }) }
– Kushagra Sharma Commented Apr 16, 2019 at 7:32But with puppeteer's browser there is, rigth?
No, there is still no document, Chromium has a document, and that's what puppeteer is controlling. Basically you just need to call click inside the chromium instance,..page.evaluate
is one way, but an easier option ispage.$('div.image').click()
. I was on mobile with my original response, so couldn't give a full response, but hoped it might have given you a hint to what was wrong.. :) – Keith Commented Apr 16, 2019 at 9:29