π§ Navigation API β Page Navigation & Info β
Control page navigation, get page information, and wait for page loads. Essential for moving around websites and capturing page state.
β οΈ Version Requirement: Binary v0.1.14+ | Library v0.0.21+
Overview β
| Method | Purpose | Use Case |
|---|---|---|
navigate() | Go to a URL | Load a page |
reload() | Refresh current page | Get fresh content |
goBack() | Previous page | Browser history |
goForward() | Next page | Browser history |
title() | Get page title | Page identification |
url() | Get current URL | Track location |
content() | Get full HTML | Page analysis |
waitForNavigation() | Wait for page load | After clicks/forms |
waitForSelector() | Wait for element | Wait for content to load |
Basic Navigation β
navigate(url?) β
Navigates to a URL. If no URL is provided, uses the registered URL from register().
// Navigate to registered URL
await piggy.books.navigate();
// Navigate to a different URL
await piggy.books.navigate("https://books.toscrape.com/catalogue/page-2.html");
await piggy.books.waitForSelector(".product_pod");
console.log("Page 2:", piggy.books.url());
// Page 2: https://books.toscrape.com/catalogue/page-2.html2
3
4
5
6
7
8
reload() β
Reloads the current page.
await piggy.books.reload();
console.log("Reloaded:", await piggy.books.title());
// Reloaded: All products | Books to Scrape - Sandbox2
3
goBack() β
Goes back one page in history.
await piggy.books.goBack();
console.log("Back to:", piggy.books.url());
// Back to: https://books.toscrape.com/2
3
goForward() β
Goes forward one page in history.
await piggy.books.goForward();
console.log("Forward to:", piggy.books.url());
// Forward to: https://books.toscrape.com/catalogue/page-2.html2
3
Page Information β
title() β
Returns the current page title. Async.
const title = await piggy.books.title();
console.log("Page title:", title);
// Page title: All products | Books to Scrape - Sandbox2
3
url() β
Returns the current URL. Synchronous β no await needed.
console.log("Page 1:", piggy.books.url());
// Page 1: https://books.toscrape.com2
content() β
Returns the full HTML of the page.
const html = await piggy.books.content();
console.log("HTML preview:", html.slice(0, 200));
// HTML preview: <!DOCTYPE html><!--[if lt IE 7]>...2
3
Waiting β
waitForSelector(selector, timeout?) β
Waits for an element to appear in the DOM. Default timeout 10000ms.
await piggy.books.waitForSelector(".product_pod");
console.log("Products visible β");2
waitForNavigation() β
Waits for the page to finish loading after an action that triggers navigation.
await piggy.books.click("a[href='/catalogue/page-2.html']");
await piggy.books.waitForNavigation();
console.log(piggy.books.url());2
3
Custom Wait with evaluate() β
For complex conditions, use evaluate() with a Promise:
// Wait for exactly 20 products to load
await piggy.books.evaluate(() => {
return new Promise<void>((resolve) => {
const check = () => {
if (document.querySelectorAll(".product_pod").length >= 20) resolve();
else setTimeout(check, 100);
};
check();
});
});
console.log("All 20 products loaded β");2
3
4
5
6
7
8
9
10
11
Full Example β
import piggy from "nothing-browser";
import path from "path";
const binaryPath = path.resolve(import.meta.dir, "../a/nothing-browser-headful.exe");
await piggy.launch({ mode: "tab", binary: binaryPath });
await piggy.register("books", "https://books.toscrape.com");
// Navigate to registered URL
await piggy.books.navigate();
console.log("Page 1:", piggy.books.url());
// Navigate to a different URL
await piggy.books.navigate("https://books.toscrape.com/catalogue/page-2.html");
await piggy.books.waitForSelector(".product_pod");
console.log("Page 2:", piggy.books.url());
// Go back
await piggy.books.goBack();
console.log("Back to:", piggy.books.url());
// Go forward
await piggy.books.goForward();
console.log("Forward to:", piggy.books.url());
// Reload
await piggy.books.reload();
console.log("Reloaded:", await piggy.books.title());
// Wait for elements
await piggy.books.waitForSelector(".product_pod");
console.log("Products visible β");
// Custom JS condition
await piggy.books.evaluate(() => {
return new Promise<void>((resolve) => {
const check = () => {
if (document.querySelectorAll(".product_pod").length >= 20) resolve();
else setTimeout(check, 100);
};
check();
});
});
console.log("All 20 products loaded β");
// Get full page HTML
const html = await piggy.books.content();
console.log("\nHTML preview:", html.slice(0, 200));
await piggy.close();2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
Expected Output β
Page 1: https://books.toscrape.com
Page 2: https://books.toscrape.com/catalogue/page-2.html
Back to: https://books.toscrape.com/
Forward to: https://books.toscrape.com/catalogue/page-2.html
Reloaded: All products | Books to Scrape - Sandbox
Products visible β
All 20 products loaded β
HTML preview: <!DOCTYPE html><!--[if lt IE 7]> <html lang="en-us" class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->...2
3
4
5
6
7
8
9
API Reference β
| Method | Parameters | Returns | Description |
|---|---|---|---|
navigate(url?) | url?: string | Promise<void> | Navigate to URL |
reload() | β | Promise<void> | Reload current page |
goBack() | β | Promise<void> | Go back in history |
goForward() | β | Promise<void> | Go forward in history |
waitForNavigation() | β | Promise<void> | Wait for page load |
title() | β | Promise<string> | Get page title |
url() | β | string (sync) | Get current URL |
content() | β | Promise<string> | Get full page HTML |
waitForSelector(selector, timeout?) | selector: string, timeout?: number | Promise<void> | Wait for element |
Next Steps β
- Interactions API β Click, type, hover
- Find API β Query DOM elements
Nothing Ecosystem Β· Ernest Tech House Β· Kenya Β· 2026