Core API Reference
The power of Puppeteer lies in its ability to simulate all of a real person's behaviors through code. This chapter explains several of the most commonly used APIs to help you quickly master page operations.
1. Page Navigation
Navigating to a specified website is the first step of automation.
javascript
await page.goto('https://www.google.com');You can wait for different stages of page loading:
networkidle0: Network requests have completely stopped (no more than 0 connections for 500ms).networkidle2: Network requests have almost stopped (no more than 2 connections for 500ms).
2. Element Interaction
Click
Enter a CSS selector to simulate a mouse click.
javascript
await page.click('#login-button');Type
Simulate a keyboard to enter text.
javascript
await page.type('#username', 'my_account');3. Waiting Mechanism
Webpages often load data dynamically, so we must wait for elements to appear before operating on them.
javascript
await page.waitForSelector('.success-message');4. Evaluation (Invoking Browser-side JS)
Sometimes, we need to execute code directly within the browser window to get information.
javascript
const title = await page.evaluate(() => {
return document.title;
});TIP
Theory mastered? The next chapter Real-world Examples will take you into actual business scenarios.
