无效的选择器/错误:找不到选择器的节点
我正在尝试使用我的代码使用 puppeteer 单击按钮,但由于某种原因,该按钮始终无法找到(无效选择器/错误:找不到选择器节点)或 waitFor 过期,这是我的代码:
const puppeteer = require('puppeteer');
const product_url = "https://www.amazon.co.uk/Usoun-Multi-Angle-Heat-Vent-Adjustable-Compatible/dp/B086HNM8F7/ref=sr_1_7?crid=16VVWBHJOOAYE&keywords=laptop+stand+adjustable&qid=1645642193&sprefix=laptop+stand+adjustable%2Caps%2C75&sr=8-7"
async function givePage(){
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
return page;
}
async function buyNow(page){
await page.goto(product_url);
await page.waitFor(20000);
await page.click("button[class='a-button a-button-oneclick a-button-icon onml-buy-now-
button buybox-button-enhancement-size']", elem => elem.click());
}
async function checkout(){
var page = await givePage();
await buyNow(page);
}
checkout();
I am trying use my code to click on a button using puppeteer but for some reason the button is always unable to be found (Not valid Selector/Error: No node found for selector) or the waitFor expires, this is my code:
const puppeteer = require('puppeteer');
const product_url = "https://www.amazon.co.uk/Usoun-Multi-Angle-Heat-Vent-Adjustable-Compatible/dp/B086HNM8F7/ref=sr_1_7?crid=16VVWBHJOOAYE&keywords=laptop+stand+adjustable&qid=1645642193&sprefix=laptop+stand+adjustable%2Caps%2C75&sr=8-7"
async function givePage(){
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
return page;
}
async function buyNow(page){
await page.goto(product_url);
await page.waitFor(20000);
await page.click("button[class='a-button a-button-oneclick a-button-icon onml-buy-now-
button buybox-button-enhancement-size']", elem => elem.click());
}
async function checkout(){
var page = await givePage();
await buyNow(page);
}
checkout();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
运行此代码时,我遇到了一些问题:
在它的父元素上。
幸运的是,亚马逊在其元素中使用了大量的
id
,我建议您只要 id 在页面加载之间保持不变(通常是这样!),就始终使用该 id。这是固定代码
I experienced few problems when running this code:
on a parent element of it.
Luckily amazon uses lots of
id's
for their elements and I suggest you to always use the id whenever the id stays the same between page loads (it normally does!).Here is the fixed code