Skip to content

Commit bf823a3

Browse files
kamathhitesh-ag1
andauthored
Continue #652 - Add Key Press (#661)
* feat: detect key press * changeset --------- Co-authored-by: Hitesh Agarwal <[email protected]>
1 parent c630373 commit bf823a3

File tree

6 files changed

+59
-1
lines changed

6 files changed

+59
-1
lines changed

Diff for: .changeset/seven-humans-make.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@browserbasehq/stagehand": patch
3+
---
4+
5+
fix press enter

Diff for: examples/google_enter.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* This file is meant to be used as a scratchpad for developing new evals.
3+
* To create a Stagehand project with best practices and configuration, run:
4+
*
5+
* npx create-browser-app@latest my-browser-app
6+
*/
7+
8+
import { Stagehand } from "@/dist";
9+
import StagehandConfig from "@/stagehand.config";
10+
11+
async function example() {
12+
const stagehand = new Stagehand({
13+
...StagehandConfig,
14+
});
15+
await stagehand.init();
16+
const page = stagehand.page;
17+
await page.goto("https://google.com");
18+
await page.act("type in 'Browserbase'");
19+
await page.act("press enter");
20+
await stagehand.close();
21+
}
22+
23+
(async () => {
24+
await example();
25+
})();

Diff for: examples/try_wordle.ts

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Stagehand } from "@/dist";
2+
import StagehandConfig from "@/stagehand.config";
3+
4+
async function example() {
5+
const stagehand = new Stagehand({
6+
...StagehandConfig,
7+
});
8+
await stagehand.init();
9+
const page = stagehand.page;
10+
await page.goto("https://www.nytimes.com/games/wordle/index.html");
11+
await page.act("click 'Continue'");
12+
await page.act("click 'Play'");
13+
await page.act("click cross sign on top right of 'How To Play' card");
14+
const word = "WORDS";
15+
for (const letter of word) {
16+
await page.act(`press ${letter}`);
17+
}
18+
await page.act("press enter");
19+
await stagehand.close();
20+
}
21+
22+
(async () => {
23+
await example();
24+
})();

Diff for: lib/prompt.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ export function buildActObservePrompt(
201201
If the action is completely unrelated to a potential action to be taken on the page, return an empty array.
202202
ONLY return one action. If multiple actions are relevant, return the most relevant one.
203203
If the user is asking to scroll to a position on the page, e.g., 'halfway' or 0.75, etc, you must return the argument formatted as the correct percentage, e.g., '50%' or '75%', etc.
204-
If the user is asking to scroll to the next chunk/previous chunk, choose the nextChunk/prevChunk method. No arguments are required here.`;
204+
If the user is asking to scroll to the next chunk/previous chunk, choose the nextChunk/prevChunk method. No arguments are required here.
205+
If the action implies a key press, e.g., 'press enter', 'press a', 'press space', etc., always choose the press method with the appropriate key as argument — e.g. 'a', 'Enter', 'Space'. Do not choose a click action on an on-screen keyboard. Capitalize the first character like 'Enter', 'Tab', 'Escape' only for special keys.`;
205206

206207
// Add variable names (not values) to the instruction if any
207208
if (variables && Object.keys(variables).length > 0) {

Diff for: package.json

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
"actionable_observe_example": "npm run build && tsx examples/actionable_observe_example.ts",
2020
"form-filling-sensible-cerebras": "npm run build && tsx examples/form_filling_sensible_cerebras.ts",
2121
"form-filling-sensible-openai": "npm run build && tsx examples/form_filling_sensible_openai.ts",
22+
"google-enter": "npm run build && tsx examples/google_enter.ts",
23+
"try-wordle": "npm run build && tsx examples/try_wordle.ts",
2224
"format": "prettier --write .",
2325
"prettier": "prettier --check .",
2426
"prettier:fix": "prettier --write .",

Diff for: types/act.ts

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export enum SupportedPlaywrightAction {
3535
CLICK = "click",
3636
FILL = "fill",
3737
TYPE = "type",
38+
PRESS = "press",
3839
SCROLL = "scrollTo",
3940
NEXT_CHUNK = "nextChunk",
4041
PREV_CHUNK = "prevChunk",

0 commit comments

Comments
 (0)