Warm tip: This article is reproduced from stackoverflow.com, please click
puppeteer screen-scraping

Unable to click element with Puppeteer

发布于 2020-04-11 22:24:02

URL : https://auto.ru/catalog/cars/all/?page_num=1

JS code:

const puppeteer = require('puppeteer');

(async () => {
    try {
        const browser = await puppeteer.launch({args: ['--no-sandbox']});
        const page = await browser.newPage();
        await page.goto('https://auto.ru/catalog/cars/all/?page_num=1', {waitUntil: 'networkidle2'});

        await page.click('#confirm-button');

        await page.waitFor(1000);

        await page.screenshot({path: './data/example.png'});

        await browser.close();

    } catch (err) {
        console.error(err);
    }
})();

When I run this code there are no errors but also there is no click, I stay at the same page.

enter image description here How can I make a click to the left blue button with Puppeteer (open the URL with incognito mode to see the buttons)?

Questioner
mixa_ru
Viewed
66
mbit 2020-02-03 05:47

It is working fine. You just need to wait for the navigation to complete before taking the snapshot. Also, always make sure the element appears before trying to click on it:

await page.waitForSelector("#confirm-button");
await Promise.all([
    page.click('#confirm-button'), 
    page.waitForNavigation()
]);
await page.screenshot({path: './data/example.png'});