未定义Node.js puppeteer";文档
本文介绍了未定义Node.js puppeteer";文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用没有ID或类的代码单击按钮,但我的终端总是响应:
document.getElementsByTagName("Accept Cookies");
^
ReferenceError: document is not defined
这是我的代码:
const puppeteer = require('puppeteer');
const product_url = "https://www.nike.com/launch"
async function givePage() {
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
return page;
}
async function acceptCookies(page) {
await page.goto(product_url);
const btn = await page.waitForSelector('#cookie-settings-layout > div > div > div >
div:nth-child(3) > div.ncss-col-md-6.ncss-col-sm-12.mb5-sm > button')
await btn.click()
}
async function notifyMe(page) {
await page.goto(product_url);
document.querySelector("button[type="submit"]").click("Notify Me");
}
async function checkout() {
var page = await givePage();
await acceptCookies(page);
await notifyMe(page);
}
checkout();
我做错了什么,我如何解决这个问题?
推荐答案
您的代码中已经有一个关于如何访问元素的示例。使用page.waitForSelector
而不是document.querySelector
,就像您在第12行所做的那样。
document.querySelector('button[type="submit"]').click()
应
(await page.waitForSelector('button[type="submit"]')).click()
这篇关于未定义Node.js puppeteer";文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,