You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Act on a specific page (when you need to target a page that isn't currently active)
43
+
await stagehand.act("click the sign in button", { page: page2 });
41
44
```
42
45
43
-
Act `action` should be as atomic and specific as possible, i.e. "Click the sign in button" or "Type 'hello' into the search input". Avoid actions that are too broad, i.e. "Order me pizza" or "Send an email to Paul asking him to call me". Actions work best for Playwright code that is vulnerable to unexpected DOM changes.
46
+
**Important:** Act instructions should be atomic and specific:
47
+
48
+
- ✅ Good: "Click the sign in button" or "Type 'hello' into the search input"
49
+
- ❌ Bad: "Order me pizza" or "Type in the search bar and hit enter" (multi-step)
50
+
51
+
### Observe + Act Pattern (Recommended)
52
+
53
+
Cache the results of `observe` to avoid unexpected DOM changes:
54
+
55
+
```typescript
56
+
const instruction = "Click the sign in button";
44
57
45
-
When using `act`, write Playwright code FIRST, then wrap it with a try-catch block where the catch block is `act`.
const actions = await stagehand.observe("select blue as the favorite color", {
69
+
page: page2,
70
+
});
71
+
await stagehand.act(actions[0], { page: page2 });
72
+
```
46
73
47
74
## Extract
48
75
49
-
If you are writing code that needs to extract data from the page, use Stagehand `extract` like this:
76
+
Extract data from pages using natural language instructions. The `extract` method is called on the `stagehand` instance.
77
+
78
+
### Basic Extraction (with schema)
50
79
51
80
```typescript
52
-
const data = await page.extract({
53
-
instruction: "extract the sign in button text",
54
-
schema: z.object({
55
-
text: z.string(),
81
+
import { z } from "zod/v3";
82
+
83
+
// Extract with explicit schema
84
+
const data = await stagehand.extract(
85
+
"extract all apartment listings with prices and addresses",
86
+
z.object({
87
+
listings: z.array(
88
+
z.object({
89
+
price: z.string(),
90
+
address: z.string(),
91
+
}),
92
+
),
56
93
}),
57
-
useTextExtract: true,
58
-
});
94
+
);
95
+
96
+
console.log(data.listings);
59
97
```
60
98
61
-
`schema` is a Zod schema that describes the data you want to extract. To extract an array, make sure to pass in a single object that contains the array, as follows:
99
+
### Simple Extraction (without schema)
62
100
63
101
```typescript
64
-
const data = await page.extract({
65
-
instruction: "extract the text inside all buttons",
66
-
schema: z.object({
67
-
text: z.array(z.string()),
102
+
// Extract returns a default object with 'extraction' field
103
+
const result = await stagehand.extract("extract the sign in button text");
104
+
105
+
console.log(result);
106
+
// Output: { extraction: "Sign in" }
107
+
108
+
// Or destructure directly
109
+
const { extraction } = await stagehand.extract(
110
+
"extract the sign in button text",
111
+
);
112
+
console.log(extraction); // "Sign in"
113
+
```
114
+
115
+
### Targeted Extraction
116
+
117
+
Extract data from a specific element using a selector:
Copy file name to clipboardExpand all lines: README.md
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,14 +8,14 @@ You can build your own web agent using: `npx create-browser-app`!
8
8
9
9
## Setting the Stage
10
10
11
-
Stagehand is an SDK for automating browsers. It's built on top of [Playwright](https://playwright.dev/) and provides a higher-level API for better debugging and AI fail-safes.
11
+
Stagehand is an SDK for automating browsers. It's built directly on top of [CDP](https://chromedevtools.github.io/devtools-protocol/) and provides a higher-level API for better debugging and AI fail-safes.
12
12
13
13
## Curtain Call
14
14
15
15
Get ready for a show-stopping development experience. Just run:
16
16
17
17
```bash
18
-
npm install &&npm run dev
18
+
pnpm install &&pnpm dev
19
19
```
20
20
21
21
## What's Next?
@@ -40,8 +40,8 @@ We have custom .cursorrules for this project. It'll help quite a bit with writin
40
40
41
41
To run on Browserbase, add your API keys to .env and change `env: "LOCAL"` to `env: "BROWSERBASE"` in [stagehand.config.ts](stagehand.config.ts).
42
42
43
-
### Use Anthropic Claude 3.5 Sonnet
43
+
### Use Anthropic Claude 4.5 Sonnet
44
44
45
45
1. Add your API key to .env
46
-
2. Change `modelName: "gpt-4o"` to `modelName: "claude-3-5-sonnet-latest"` in [stagehand.config.ts](stagehand.config.ts)
46
+
2. Change `modelName: "gpt-4o"` to `modelName: "claude-sonnet-4-5"` in [stagehand.config.ts](stagehand.config.ts)
47
47
3. Change `modelClientOptions: { apiKey: process.env.OPENAI_API_KEY }` to `modelClientOptions: { apiKey: process.env.ANTHROPIC_API_KEY }` in [stagehand.config.ts](stagehand.config.ts)
0 commit comments