-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstage-hand.js
More file actions
126 lines (109 loc) · 4.06 KB
/
Copy pathstage-hand.js
File metadata and controls
126 lines (109 loc) · 4.06 KB
1
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Generated script for workflow de99949f-9ad3-4e79-9156-e7c98ad66702
// Generated at 2025-08-31T13:06:43.786Z
import { Stagehand, type ConstructorParams } from '@browserbasehq/stagehand';
import { z } from 'zod';
// Stagehand configuration
const stagehandConfig = (): ConstructorParams => {
return {
env: 'BROWSERBASE',
verbose: 1,
modelName: 'google/gemini-2.5-flash-preview-05-20',
modelClientOptions: {
apiKey: process.env.GOOGLE_API_KEY,
},
};
};
async function runWorkflow() {
let stagehand: Stagehand | null = null;
try {
// Initialize Stagehand
console.log('Initializing Stagehand...');
stagehand = new Stagehand(stagehandConfig());
await stagehand.init();
console.log('Stagehand initialized successfully.');
// Get the page instance
const page = stagehand.page;
if (!page) {
throw new Error('Failed to get page instance from Stagehand');
}
const variables = {
input_de9994_1: 'Nintendo Switch OLED console',
};
// Step 1: Navigate to URL
console.log('Navigating to: https://amazon.com/');
await page.goto('https://amazon.com/');
// Step 2: Perform action
console.log(
`Performing action: type ${variables.input_de9994_1} into the search box`,
);
await page.act({
description: `type ${variables.input_de9994_1} into the search box`,
});
// Step 3: Perform action
console.log(`Performing action: click the search button`);
await page.act({
description: `click the search button`,
});
// Step 4: Extract data
console.log(
`Extracting: extract all Nintendo Switch OLED console products visible on this page with their names, prices, and availability status`,
);
const extractedData4 = await page.extract({
instruction: `extract all Nintendo Switch OLED console products visible on this page with their names, prices, and availability status`,
schema: z.object({
products: z.array(
z.object({
name: z.string().optional(),
price: z.number().optional(),
availability: z.string().optional(),
condition: z.string().optional(),
}),
),
}),
});
console.log('Extracted:', extractedData4);
// Step 5: Perform action
console.log(
`Performing action: click the Nintendo Switch – OLED Model w/White Joy-Con product`,
);
await page.act({
description: `click the Nintendo Switch – OLED Model w/White Joy-Con product`,
});
// Step 6: Perform action
console.log(`Performing action: click the Add to Cart button`);
await page.act({
description: `click the Add to Cart button`,
});
// Step 7: Perform action
console.log(`Performing action: click the Cart button`);
await page.act({
description: `click the Cart button`,
});
// Step 8: Perform action
console.log(`Performing action: click the Proceed to checkout button`);
await page.act({
description: `click the Proceed to checkout button`,
});
console.log('Workflow completed successfully');
return { success: true };
} catch (error) {
console.error('Workflow failed:', error);
return { success: false, error };
} finally {
// Clean up
if (stagehand) {
console.log('Closing Stagehand connection.');
try {
await stagehand.close();
} catch (err) {
console.error('Error closing Stagehand:', err);
}
}
}
}
// Single execution
runWorkflow().then((result) => {
console.log('Execution result:', result);
process.exit(result.success ? 0 : 1);
});
export default runWorkflow;