adding an item to cart with puppetteer on amazon

Viewed 291

i started coding this app for scalping on amazon, just to give it a try, im building it with puppeteer and i need to add an item to the cart or just buy it, but it doesn't work on amazon, i tried it on the walmart site and it's perfect.

const puppeteer = require('puppeteer');

const product_url = "https://www.amazon.it/Gigabyte-GeForce-RTX-3060-GAMING/dp/B08WB6R2K4/ref=sr_1_3?__mk_it_IT=%C3%85M%C3%85%C5%BD%C3%95%C3%91&dchild=1&keywords=rtx&qid=1628563186&sr=8-3";



async function givePage(){
    const browser = await puppeteer.launch({headless: false});
    const page = await browser.newPage();
    return page;
}

async function AddToCart(page){
    await page.goto(product_url);
    await page.waitForSelector("button[inputid='add-to-cart-button'")
    await page.click("button[inputid='add-to-cart-button']", elem => elem.click());
    
}

async function checkout(){
    var page = await givePage();
    await AddToCart(page);  
}


checkout();

1 Answers

i resolved the problem creating an array with the XPath:

await page.$x('//*[@id="submit.buy-now-announce"]');

then told the program to find the button (for me it was the first you may change it).

    const elements = await page.$x('//*[@id="submit.buy-now-announce"]');
    await elements[0].click() ;

Related