Manyou
Manyou

Reputation: 19

How can i click the following Button with puppeteer?

I'm trying to click a button on a website with puppeteer but it doesn't work for me.

Element-info:

<button aria-label="Alles akzeptieren" role="button" data-testid="uc-accept-all-button" class="sc-gtsrHT gqGzpd">OK</button>

My Code:

async function checkout(){
    const browser = await puppeteer.launch({headless: false});
    const page = await browser.newPage();
    
    
    await page.goto(product_url);
    await page.waitFor(3000);
    await page.click("Button[class='sc-gtsrHT gqGzpd']", elem => elem.click());

}

Error Message:

Error: No node found for selector: Button[class='sc-gtsrHT gqGzpd'] at Object.assert (C:\Coding\Ticket-Bot\node_modules\puppeteer\lib\cjs\puppeteer\common\assert.js:26:15)
at DOMWorld.click (C:\Coding\Ticket-Bot\node_modules\puppeteer\lib\cjs\puppeteer\common\DOMWorld.js:277:21)
at processTicksAndRejections (internal/process/task_queues.js:95:5) at async checkout (C:\Coding\Ticket-Bot\bayern.js:14:5)

Pictures:

what is the correct code so that the button will be clicked?

Upvotes: 0

Views: 2840

Answers (1)

theDavidBarton
theDavidBarton

Reputation: 8851

You get the error due to the element does not exist on the page, it may be caused by the fact that CSS classes are autogenerated as @Sirko suggests.

You can even inspect the element's class name in DevTools if you launch puppeteer in headful mode.

You will need to find those selectors that will remain the same, e.g.:

await page.click('[aria-label="Alles akzeptieren"]');
await page.click('[data-testid="uc-accept-all-button"]');

Note: I am not sure if you need elem => elem.click() in the click options.

Upvotes: 2

Related Questions