|
| 1 | +# Browser Automation Examples |
| 2 | + |
| 3 | +This document provides detailed examples of common browser automation tasks using the Stagehand Skill. |
| 4 | + |
| 5 | +## Example 1: Extract Product Information from E-commerce |
| 6 | + |
| 7 | +**User request**: "Go to example.com/product/123 and extract the product details" |
| 8 | + |
| 9 | +**Workflow**: |
| 10 | + |
| 11 | +1. **Navigate** to the product page: |
| 12 | + ``` |
| 13 | + Tool: mcp__stagehand__navigate |
| 14 | + URL: https://example.com/product/123 |
| 15 | + ``` |
| 16 | + |
| 17 | +2. **Extract** product data with schema: |
| 18 | + ``` |
| 19 | + Tool: mcp__stagehand__extract |
| 20 | + Instruction: "Extract the product information" |
| 21 | + Schema: { |
| 22 | + "productName": "string", |
| 23 | + "price": "number", |
| 24 | + "currency": "string", |
| 25 | + "inStock": "boolean", |
| 26 | + "rating": "number", |
| 27 | + "reviewCount": "number" |
| 28 | + } |
| 29 | + ``` |
| 30 | + |
| 31 | +3. **Close** the browser: |
| 32 | + ``` |
| 33 | + Tool: mcp__stagehand__close_browser |
| 34 | + ``` |
| 35 | + |
| 36 | +**Expected result**: JSON object with product details that can be analyzed or stored. |
| 37 | + |
| 38 | +--- |
| 39 | + |
| 40 | +## Example 2: Fill Out and Submit a Contact Form |
| 41 | + |
| 42 | +**User request**: "Fill out the contact form on example.com with my information" |
| 43 | + |
| 44 | +**Workflow**: |
| 45 | + |
| 46 | +1. **Navigate** to contact page: |
| 47 | + ``` |
| 48 | + Tool: mcp__stagehand__navigate |
| 49 | + URL: https://example.com/contact |
| 50 | + ``` |
| 51 | + |
| 52 | +2. **Act**: Fill in name field: |
| 53 | + ``` |
| 54 | + Tool: mcp__stagehand__act |
| 55 | + Action: "Fill in the name field with 'John Doe'" |
| 56 | + ``` |
| 57 | + |
| 58 | +3. **Act**: Fill in email field: |
| 59 | + ``` |
| 60 | + Tool: mcp__stagehand__act |
| 61 | + Action: "Fill in the email field with 'john.doe@example.com'" |
| 62 | + ``` |
| 63 | + |
| 64 | +4. **Act**: Fill in message field: |
| 65 | + ``` |
| 66 | + Tool: mcp__stagehand__act |
| 67 | + Action: "Fill in the message field with 'I would like to inquire about your services'" |
| 68 | + ``` |
| 69 | + |
| 70 | +5. **Act**: Submit the form: |
| 71 | + ``` |
| 72 | + Tool: mcp__stagehand__act |
| 73 | + Action: "Click the Submit button" |
| 74 | + ``` |
| 75 | + |
| 76 | +6. **Screenshot** to capture confirmation: |
| 77 | + ``` |
| 78 | + Tool: mcp__stagehand__screenshot |
| 79 | + ``` |
| 80 | + |
| 81 | +7. **Close** the browser: |
| 82 | + ``` |
| 83 | + Tool: mcp__stagehand__close_browser |
| 84 | + ``` |
| 85 | + |
| 86 | +--- |
| 87 | + |
| 88 | +## Example 3: Research and Summarize News Articles |
| 89 | + |
| 90 | +**User request**: "Check the latest tech news on techcrunch.com and summarize the top stories" |
| 91 | + |
| 92 | +**Workflow**: |
| 93 | + |
| 94 | +1. **Navigate** to news site: |
| 95 | + ``` |
| 96 | + Tool: mcp__stagehand__navigate |
| 97 | + URL: https://techcrunch.com |
| 98 | + ``` |
| 99 | + |
| 100 | +2. **Extract** article headlines and summaries: |
| 101 | + ``` |
| 102 | + Tool: mcp__stagehand__extract |
| 103 | + Instruction: "Extract the top 5 article headlines and their summaries" |
| 104 | + Schema: { |
| 105 | + "headlines": "string", |
| 106 | + "summary": "string", |
| 107 | + "author": "string", |
| 108 | + "publishedDate": "string" |
| 109 | + } |
| 110 | + ``` |
| 111 | + |
| 112 | +3. **Close** the browser: |
| 113 | + ``` |
| 114 | + Tool: mcp__stagehand__close_browser |
| 115 | + ``` |
| 116 | + |
| 117 | +4. Analyze and summarize the extracted data using Claude's text analysis capabilities. |
| 118 | + |
| 119 | +--- |
| 120 | + |
| 121 | +## Example 4: Login and Navigate Authenticated Area |
| 122 | + |
| 123 | +**User request**: "Log into example.com and navigate to my dashboard" |
| 124 | + |
| 125 | +**Workflow**: |
| 126 | + |
| 127 | +1. **Navigate** to login page: |
| 128 | + ``` |
| 129 | + Tool: mcp__stagehand__navigate |
| 130 | + URL: https://example.com/login |
| 131 | + ``` |
| 132 | + |
| 133 | +2. **Act**: Fill in username: |
| 134 | + ``` |
| 135 | + Tool: mcp__stagehand__act |
| 136 | + Action: "Fill in the username field with 'myusername'" |
| 137 | + ``` |
| 138 | + |
| 139 | +3. **Act**: Fill in password: |
| 140 | + ``` |
| 141 | + Tool: mcp__stagehand__act |
| 142 | + Action: "Fill in the password field with 'mypassword'" |
| 143 | + ``` |
| 144 | + |
| 145 | +4. **Act**: Click login button: |
| 146 | + ``` |
| 147 | + Tool: mcp__stagehand__act |
| 148 | + Action: "Click the Login button" |
| 149 | + ``` |
| 150 | + |
| 151 | +5. **Act**: Wait for page load: |
| 152 | + ``` |
| 153 | + Tool: mcp__stagehand__act |
| 154 | + Action: "Wait for the page to fully load" |
| 155 | + ``` |
| 156 | + |
| 157 | +6. **Navigate** to dashboard: |
| 158 | + ``` |
| 159 | + Tool: mcp__stagehand__navigate |
| 160 | + URL: https://example.com/dashboard |
| 161 | + ``` |
| 162 | + |
| 163 | +7. **Screenshot** the dashboard: |
| 164 | + ``` |
| 165 | + Tool: mcp__stagehand__screenshot |
| 166 | + ``` |
| 167 | + |
| 168 | +8. **Close** the browser: |
| 169 | + ``` |
| 170 | + Tool: mcp__stagehand__close_browser |
| 171 | + ``` |
| 172 | + |
| 173 | +**Note**: This example uses Chrome's user profile (`.chrome-profile/`) which may preserve session cookies between runs. |
| 174 | + |
| 175 | +--- |
| 176 | + |
| 177 | +## Example 5: Search and Collect Results |
| 178 | + |
| 179 | +**User request**: "Search Google for 'best TypeScript practices' and get the top 5 results" |
| 180 | + |
| 181 | +**Workflow**: |
| 182 | + |
| 183 | +1. **Navigate** to Google: |
| 184 | + ``` |
| 185 | + Tool: mcp__stagehand__navigate |
| 186 | + URL: https://www.google.com |
| 187 | + ``` |
| 188 | + |
| 189 | +2. **Act**: Perform search: |
| 190 | + ``` |
| 191 | + Tool: mcp__stagehand__act |
| 192 | + Action: "Type 'best TypeScript practices' in the search box and press Enter" |
| 193 | + ``` |
| 194 | + |
| 195 | +3. **Act**: Wait for results: |
| 196 | + ``` |
| 197 | + Tool: mcp__stagehand__act |
| 198 | + Action: "Wait for search results to load" |
| 199 | + ``` |
| 200 | + |
| 201 | +4. **Extract** search results: |
| 202 | + ``` |
| 203 | + Tool: mcp__stagehand__extract |
| 204 | + Instruction: "Extract the top 5 search results" |
| 205 | + Schema: { |
| 206 | + "title": "string", |
| 207 | + "url": "string", |
| 208 | + "snippet": "string" |
| 209 | + } |
| 210 | + ``` |
| 211 | + |
| 212 | +5. **Close** the browser: |
| 213 | + ``` |
| 214 | + Tool: mcp__stagehand__close_browser |
| 215 | + ``` |
| 216 | + |
| 217 | +--- |
| 218 | + |
| 219 | +## Example 6: Download a File |
| 220 | + |
| 221 | +**User request**: "Download the PDF file from example.com/documents/report.pdf" |
| 222 | + |
| 223 | +**Workflow**: |
| 224 | + |
| 225 | +1. **Navigate** to the file URL: |
| 226 | + ``` |
| 227 | + Tool: mcp__stagehand__navigate |
| 228 | + URL: https://example.com/documents/report.pdf |
| 229 | + ``` |
| 230 | + |
| 231 | +2. **Act**: Wait for download to start: |
| 232 | + ``` |
| 233 | + Tool: mcp__stagehand__act |
| 234 | + Action: "Wait for 5 seconds for the download to complete" |
| 235 | + ``` |
| 236 | + |
| 237 | +3. **Close** the browser: |
| 238 | + ``` |
| 239 | + Tool: mcp__stagehand__close_browser |
| 240 | + ``` |
| 241 | + |
| 242 | +**Note**: Files are automatically downloaded to `./agent/downloads/` directory due to CDP configuration in the Stagehand initialization. |
| 243 | + |
| 244 | +--- |
| 245 | + |
| 246 | +## Example 7: Debugging a Page Issue |
| 247 | + |
| 248 | +**User request**: "Check why the submit button isn't working on example.com/form" |
| 249 | + |
| 250 | +**Workflow**: |
| 251 | + |
| 252 | +1. **Navigate** to the form page: |
| 253 | + ``` |
| 254 | + Tool: mcp__stagehand__navigate |
| 255 | + URL: https://example.com/form |
| 256 | + ``` |
| 257 | + |
| 258 | +2. **Screenshot** initial state: |
| 259 | + ``` |
| 260 | + Tool: mcp__stagehand__screenshot |
| 261 | + ``` |
| 262 | + |
| 263 | +3. **Observe** available elements: |
| 264 | + ``` |
| 265 | + Tool: mcp__stagehand__observe |
| 266 | + Query: "Find all buttons and their states" |
| 267 | + ``` |
| 268 | + |
| 269 | +4. **Observe** form fields: |
| 270 | + ``` |
| 271 | + Tool: mcp__stagehand__observe |
| 272 | + Query: "Find all form input fields and their required status" |
| 273 | + ``` |
| 274 | + |
| 275 | +5. **Act**: Try filling required fields: |
| 276 | + ``` |
| 277 | + Tool: mcp__stagehand__act |
| 278 | + Action: "Fill in all required fields with test data" |
| 279 | + ``` |
| 280 | + |
| 281 | +6. **Screenshot** after filling: |
| 282 | + ``` |
| 283 | + Tool: mcp__stagehand__screenshot |
| 284 | + ``` |
| 285 | + |
| 286 | +7. **Observe** button state again: |
| 287 | + ``` |
| 288 | + Tool: mcp__stagehand__observe |
| 289 | + Query: "Check if the submit button is now enabled" |
| 290 | + ``` |
| 291 | + |
| 292 | +8. **Close** the browser: |
| 293 | + ``` |
| 294 | + Tool: mcp__stagehand__close_browser |
| 295 | + ``` |
| 296 | + |
| 297 | +Analyze the screenshots and observations to determine the issue. |
| 298 | + |
| 299 | +--- |
| 300 | + |
| 301 | +## Example 8: Multi-Page Data Collection |
| 302 | + |
| 303 | +**User request**: "Extract product information from the first 3 pages of results on example.com/products" |
| 304 | + |
| 305 | +**Workflow**: |
| 306 | + |
| 307 | +1. **Navigate** to products page: |
| 308 | + ``` |
| 309 | + Tool: mcp__stagehand__navigate |
| 310 | + URL: https://example.com/products |
| 311 | + ``` |
| 312 | + |
| 313 | +2. **Extract** products from page 1: |
| 314 | + ``` |
| 315 | + Tool: mcp__stagehand__extract |
| 316 | + Instruction: "Extract all products on this page" |
| 317 | + Schema: { |
| 318 | + "name": "string", |
| 319 | + "price": "number", |
| 320 | + "imageUrl": "string" |
| 321 | + } |
| 322 | + ``` |
| 323 | + |
| 324 | +3. **Act**: Click next page: |
| 325 | + ``` |
| 326 | + Tool: mcp__stagehand__act |
| 327 | + Action: "Click the Next Page button" |
| 328 | + ``` |
| 329 | + |
| 330 | +4. **Extract** products from page 2: |
| 331 | + ``` |
| 332 | + Tool: mcp__stagehand__extract |
| 333 | + Instruction: "Extract all products on this page" |
| 334 | + Schema: { |
| 335 | + "name": "string", |
| 336 | + "price": "number", |
| 337 | + "imageUrl": "string" |
| 338 | + } |
| 339 | + ``` |
| 340 | + |
| 341 | +5. **Act**: Click next page: |
| 342 | + ``` |
| 343 | + Tool: mcp__stagehand__act |
| 344 | + Action: "Click the Next Page button" |
| 345 | + ``` |
| 346 | + |
| 347 | +6. **Extract** products from page 3: |
| 348 | + ``` |
| 349 | + Tool: mcp__stagehand__extract |
| 350 | + Instruction: "Extract all products on this page" |
| 351 | + Schema: { |
| 352 | + "name": "string", |
| 353 | + "price": "number", |
| 354 | + "imageUrl": "string" |
| 355 | + } |
| 356 | + ``` |
| 357 | + |
| 358 | +7. **Close** the browser: |
| 359 | + ``` |
| 360 | + Tool: mcp__stagehand__close_browser |
| 361 | + ``` |
| 362 | + |
| 363 | +Combine and process all extracted data. |
| 364 | + |
| 365 | +--- |
| 366 | + |
| 367 | +## Tips for Success |
| 368 | + |
| 369 | +- **Be specific with natural language**: "Click the blue Submit button in the footer" is better than "click submit". This is **extremely important** because there's much ambiguity in many websites. |
| 370 | +- **Wait when needed**: After navigation or actions that trigger page changes, explicitly wait |
| 371 | +- **Use observe for discovery**: When unsure what elements exist, use observe first |
| 372 | +- **Take screenshots for debugging**: Visual confirmation helps understand what the browser sees |
| 373 | +- **Handle errors gracefully**: If an action fails, try breaking it into smaller steps |
| 374 | +- **Clean up resources**: Always close the browser when done to free up system resources |
0 commit comments