How to click on button using Puppeteer?

Advertisements

Link: http://quotes.toscrape.com/

Click on the above link and scroll to the bottom. I don’t know how to click on the Next button.

My code:

await page.click(".pager > .next > a")

The above code is not working.

I want to know, how to click on this button.

>Solution :

I use below code and can click Next button. The keypoint here is to use waitForSelector to make sure the element you want is available.

import puppeteer from 'puppeteer';

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

  await page.goto('http://quotes.toscrape.com/');

  // Set screen size
  await page.setViewport({width: 1080, height: 1024});

  // Wait and click on first result
  const searchResultSelector = '.next a';
  await page.waitForSelector(searchResultSelector);
  await page.click(searchResultSelector);

  // await browser.close();
})();

refs from doc: https://pptr.dev/#example

Leave a ReplyCancel reply