Skip to main content

Click, wait and read: follow a link without a fixed sleep

Read the first author on a practice page, click its Next link, wait for the new page with waitForNavigation instead of a sleep, and read the first author again.

From our run on
  1. page 1, first author: Albert Einstein
  2. address: https://quotes.toscrape.com/js/page/2/
  3. page 2, first author: Marilyn Monroe
  4. session closed
Lesson 19 of 20 5 min read

To read the page a click leads to, start waiting for the navigation before you click, then run both together and read once the wait is over. In Puppeteer that is Promise.all around page.waitForNavigation() and page.click(). A fixed sleep either wastes time or ends too soon, and the wait ends when the new page arrives.

Lesson 18 opened a hosted browser session, loaded one page and read it. This lesson adds one step: the program reads the first author on https://quotes.toscrape.com/js/, clicks the Next link at the bottom, waits for page 2 and reads the first author there. By the end you will have a program that moves through a site the way a visitor does, one click at a time, and knows when each page is ready.

Why the order matters

A click that follows a link starts a navigation, and a navigation takes time. The old page unloads, the new one loads and its scripts run. If your program reads in that gap, it reads the old page, or one that is half built.

The tempting fix is to sleep for two seconds after the click. On a slow day the page takes three and the read fails. On a fast day it takes half a second and the program wastes the rest. Puppeteer has a method that waits for exactly this. Its reference says waitForNavigation "is useful when you run code that will indirectly cause the page to navigate."

There is a catch in when you call it. If you click first and start waiting after, a fast page can finish loading before the wait begins, and then the wait has nothing left to see. The reference for click names the problem: "you may end up with a race condition that yields unexpected results." The fix is to start both at once, with the wait listed first, so it is already listening when the click lands.

The program

The robots.txt check you made in lesson 17 covers the /js/ pages, page 2 included. Save this as click-and-read.mjs beside your lesson 18 file. It uses the same puppeteer-core install and the same SPIDER_API_KEY variable.

import puppeteer from "puppeteer-core";

const browser = await puppeteer.connect({
  browserWSEndpoint: `wss://browser.spider.cloud/v1/browser?token=${process.env.SPIDER_API_KEY}`,
});

const firstAuthor = (page) =>
  page.evaluate(() => document.querySelector(".quote .author").textContent);

try {
  const page = await browser.newPage();
  await page.goto("https://quotes.toscrape.com/js/");
  console.log("page 1, first author:", await firstAuthor(page));

  // Start waiting before the click, so the new page cannot arrive unseen.
  await Promise.all([page.waitForNavigation(), page.click("li.next a")]);
  console.log("address:", page.url());
  console.log("page 2, first author:", await firstAuthor(page));
} finally {
  await browser.close();
  console.log("session closed");
}

On 2026-11-20, under puppeteer-core 25.18.1, the program loaded one page and clicked once, and the four lines below are its whole output. The 2026-11-21 run matched them.

page 1, first author: Albert Einstein
address: https://quotes.toscrape.com/js/page/2/
page 2, first author: Marilyn Monroe
session closed

Read it line by line

firstAuthor is a small helper, so the program reads both pages the same way. It runs inside the tab and returns the text of the first .author element under a .quote.

page.click("li.next a") finds the link inside the list item with the class next, scrolls it into view and clicks its center, the way a mouse would. If no element matches, it throws. That is useful on the last page of a site, where there is no Next link. The program stops with an error instead of reading the same page twice.

Promise.all starts the wait and the click together and resolves when both are done. The wait is done when the new page has loaded. After that line, page.url() reads the address the browser is now on. The run shows /js/page/2/, the page the link pointed to, so the click took the program where a visitor would go.

The two authors differ, Albert Einstein and then Marilyn Monroe. That is your proof the second read saw the second page. If both lines named the same author, the read would have happened before the new page arrived.

Waiting is not the same as pacing

The program removed a sleep, and that might sound like it now loads pages as fast as it can. It does not. Each click is one more page load on someone else's server. This run made two loads, which is fine. If you loop through all ten pages of the site, put a pause between one click and the next, the way how many requests per second to send explains. A sleep to guess when a page is ready is a bug. A pause to keep a polite rate is part of the program.

Clicking Next is one way a web scraping job walks a list, and each click is a page load the site has to serve. Leave the login link alone.

Try it yourself

Extend your copy so it clicks Next a second time and reads the first author on page 3. Use the same Promise.all pattern for the second click, and pause a few seconds between the clicks. Before you run it, open https://quotes.toscrape.com/js/page/3/ in your own browser and note its first author. You are done when your program prints three different authors, an address that ends in /js/page/3/, a first author for page 3 that matches the one you noted, and session closed as its last line.

Do this, avoid this

Do this

  • Start waitForNavigation and the click together in one Promise.all, with the wait first.
  • Read page.url() after the wait to confirm where the click went.
  • Read the same element on both pages, so a stale read shows up as a repeated value.
  • Pause between clicks when the program walks through many pages.

Avoid this

  • A fixed sleep after a click to guess when the next page is ready.
  • Clicking first and starting the wait on the next line.
  • Clicking login, sign-up or buy links. This course reads what a signed-out visitor sees.

Next

Every program so far has worked. Lesson 20 points one at a page with no quotes on it, so you can watch it fail, close the session anyway and report the failure.

Sources

  1. Puppeteer docs, Page.click() method, version 25.18.1 Read
  2. Puppeteer docs, Page.waitForNavigation() method, version 25.18.1 Read

Run this on a page you care about

The playground sends the request this page describes and shows you the response. Keyless runs work without an account and are rate limited.