To drive a browser from your own program, connect a client library such as Puppeteer to the browser's WebSocket address, open a page, load your address and read what you need. Close the session in a finally block, so it ends even when a step before it fails.
In lesson 17 you found that https://quotes.toscrape.com/js/ needs a browser. The API rendered it for you and handed back finished text. That works when reading is all you want. Clicking a link, waiting for the next page or reading one element at a time needs a program that holds the page while it works. By the end of this lesson you will have that program: 17 lines that open one session, read three facts from the page and close the session again.
What connect means
Puppeteer can start a browser on your machine, or it can attach to one that is already running somewhere else. Its reference puts the second case in one line: "This method attaches Puppeteer to an existing browser instance." The two sides talk the Chrome DevTools Protocol over a WebSocket. Our browser reference describes its side as "CDP over WebSocket. Any client that speaks it works." Playwright and chromey speak it too. This lesson uses Puppeteer because its connect call is the shortest to read.
Install puppeteer-core, not puppeteer. The full package downloads a browser of its own, and here the browser is the remote one, so the core package is all you need. The saved runs used version 25.18.1.
npm install puppeteer-core@25.18.1Keep your key in the SPIDER_API_KEY environment variable, as in every lesson so far. The program reads it from there and puts it in the socket address. Never paste the key into the file. A file gets shared, committed and pasted into chat, and a key in it goes along every time.
The program
The robots.txt check from lesson 17 still holds for the /js/ path, so you can load the page. Save this as connect.mjs and run it with node connect.mjs.
import puppeteer from "puppeteer-core";
const browser = await puppeteer.connect({
browserWSEndpoint: `wss://browser.spider.cloud/v1/browser?token=${process.env.SPIDER_API_KEY}`,
});
try {
const page = await browser.newPage();
const response = await page.goto("https://quotes.toscrape.com/js/");
console.log("status:", response.status());
console.log("title:", await page.title());
const quotes = await page.evaluate(() => document.querySelectorAll(".quote").length);
console.log("quotes on the page:", quotes);
} finally {
await browser.close();
console.log("session closed");
}The saved run of this file, made on 2026-11-20 with puppeteer-core 25.18.1, printed these four lines. A second run of the same file on 2026-11-21 printed the same four.
status: 200
title: Quotes to Scrape
quotes on the page: 10
session closedRead it line by line
puppeteer.connect opens the session. When it returns, you have a browser object that stands for the remote browser, and nothing has loaded yet.
browser.newPage() opens a tab. page.goto loads the address and waits for the page to finish loading. It returns the response for the page itself, and response.status() is the same status code your /scrape calls showed in the status field. Read it first, for the same reason as in lesson 1. A 403, a 429 or a sign-in page means the site has said no, and the program should stop there.
page.title() reads the page's title. page.evaluate is where the program reaches into the page. The function you pass runs inside the browser tab, not in Node, so it can use document the way a script on the page would. It hands back one value, here the number of .quote elements. Ten matches the ten Tags: lines you counted in lesson 17, so the two ways of rendering agree.
The finally block is the part to copy into every browser program you write. A session stays open until someone closes it. If goto throws, or any later line in try fails, the lines after it never run, but finally still does. Lesson 20 breaks this program on purpose to show that.
The same rules apply
A browser session is still a visitor on someone else's site, so this program loads one page and stops. Holding a page open is the part of a web scraping job a plain request cannot do, and lesson 20 lists the rules that come with it.
Try it yourself
Change the address in goto to the second page of the practice site, https://quotes.toscrape.com/js/page/2/. Open that address in your own browser first and count the quotes. Then run your copy of the program. You are done when it prints a status of 200, the title, a count that matches the one you took by hand, and session closed as its last line.
If the count is 0, look at the page in your own browser again before you change the selector. The page may not be the one you meant to load.
Do this, avoid this
Do this
- Install puppeteer-core, which connects to a browser and does not download one.
- Read the key from SPIDER_API_KEY when the program starts.
- Read the status that goto returns before anything else on the page.
- Close the browser in a finally block, so the session ends on every path.
Avoid this
- Writing the API key into the file, where it travels with every copy.
- Opening a new session for each page when one session can load them in turn.
- Going on after a 403, a 429, a sign-in page or a challenge. The site has said no.
- Saving names, emails or other personal data the rendered page shows.
Next
This program loads one page and reads it. Lesson 19 clicks the Next link, waits for the new page without a fixed sleep and reads it again.