A browser program closes what it opens by putting browser.close() in a finally block, giving every wait a time limit, and setting a failing exit code when a step goes wrong. Then a failed read, a slow page or a missing element still ends the session, and whatever runs the program can tell the run failed.
The programs in lesson 18 and lesson 19 worked, so their finally blocks never had much to do. This lesson breaks one on purpose. You point it at a page that loads but holds no quotes, and you watch three things happen in order: the read fails, the session closes anyway, and the program exits with code 1. By the end you will have a pattern you can wrap around every browser program you write.
Two ways a step can fail
A step can fail fast, or it can hang. The fast kind throws an error. A selector matches nothing, a value is null, a click finds no link. The slow kind never answers. A page keeps loading, or the connection to the browser stops replying.
A finally block handles the fast kind on its own. The slow kind needs a limit, or the program waits forever and the finally block never gets its turn. Puppeteer gives you two limits.
The page limit covers loading. The reference for setDefaultNavigationTimeout says "This setting will change the default maximum navigation time for the following methods", and goto and waitForNavigation are on its list. The connection limit covers every message to the browser. The connect options describe protocolTimeout as "Timeout setting for individual protocol (CDP) calls." Its default is 180 seconds. This program sets both to 30.
The program
https://quotes.toscrape.com/js/page/99/ is past the end of the practice site, which has 10 pages. The server still answers it with a page, and that page has no quotes. The robots.txt check from lesson 17 covers it. Save this as close-what-you-open.mjs beside the other two files.
import puppeteer from "puppeteer-core";
const browser = await puppeteer.connect({
browserWSEndpoint: `wss://browser.spider.cloud/v1/browser?token=${process.env.SPIDER_API_KEY}`,
protocolTimeout: 30_000,
});
try {
const page = await browser.newPage();
page.setDefaultNavigationTimeout(30_000);
// The site has 10 pages of quotes. Page 99 loads, and holds none.
const response = await page.goto("https://quotes.toscrape.com/js/page/99/");
console.log("status:", response.status());
const author = await page.evaluate(() => document.querySelector(".quote .author").textContent);
console.log("first author:", author);
} catch (error) {
console.log("stopped:", error.message);
process.exitCode = 1;
} finally {
await browser.close();
console.log("session closed");
}Pointed at that address on 2026-11-20 with puppeteer-core 25.18.1, the program exited with code 1 after three lines. A 2026-11-21 run printed the same three.
status: 200
stopped: Uncaught TypeError: Cannot read properties of null (reading 'textContent')
session closedRead the failure
The first line is the trap. The server said 200, and by the status alone the page is fine. Lesson 17 showed the same thing, a 200 beside an answer that failed the check. Always check that the page holds what you came for.
The second line is the fast failure. document.querySelector(".quote .author") found nothing and returned null, and reading textContent from null throws. The catch block prints the message and sets process.exitCode to 1. Use exitCode, not process.exit(1). process.exit ends Node on the spot, and the finally block below it would never run.
The third line is the point of the lesson. The read failed, and the session still closed. That holds whatever happens inside try.
The slow kind, on a real run
On 2026-11-21 we ran the same file once more, just before the 2026-11-21 run above. That time the page did not finish loading, and the run printed this.
stopped: Navigation timeout of 30000 ms exceeded
session closedWe do not know why that load was slow. The next run started 44 seconds later and finished in 4.3 seconds. The page limit is what turned it into an error after 30 seconds, and finally closed the session as before. Without the limit, the program would have sat on that load with the session open. A program that runs on a timer would then start its next run on top of this one.
What still holds in a browser
A cloud browser session runs scripts and fetches images, but on the site's side it is still one visitor making requests. Everything the course has asked of a plain request applies:
- Read robots.txt and the site's terms before the first load.
- Keep to one page at a time, with a pause between loads, as how many requests per second to send explains.
- Stop on a 403, a 429, a sign-in page or a challenge. The site has said no.
- Leave out the personal data a rendered page shows, and respect copyright in what you keep.
- Close the session on every path, so a failed run leaves nothing open.
The web scraping guide puts these rules in the context of a whole project.
Try it yourself
Wrap your lesson 19 program in the same pattern. Add both limits, a catch that prints the message and sets process.exitCode = 1, and keep the finally. Then change its first address to https://quotes.toscrape.com/js/page/10/, the last page. Open that address in your own browser first and confirm the bottom of the page has no Next link, which means the click has nothing to find and throws. Run it, then run echo $? in the same shell. You are done when the program prints a stopped: line, then session closed, and echo $? prints 1.
Do this, avoid this
Do this
- Close the browser in a finally block in every program, even the short ones.
- Set a navigation limit and a protocol limit, so a hung step becomes an error.
- Set process.exitCode to 1 when a step fails, so the caller sees the failure.
- Check that the page holds the content you came for, not only its status.
Avoid this
- Calling process.exit inside catch, which skips the finally block.
- Swallowing an error and exiting 0, which reports a failed run as a good one.
- Retrying a failed page in a tight loop. Wait, and stop if the site said no.
- Keeping what a failed run read. Personal data on a page that did not finish loading is still personal data.
Next
This ends the browser module. You can tell when a page needs a browser, connect to one, move through a site and stop cleanly when something goes wrong. To put one of these programs on a schedule, go back to keeping every run and give it the same dated files, log and lock the crawl script has.