跳转到主要内容

使用 Cursor 规则优化 AI 建议

Stagehand 团队多数成员使用 Cursor 编写代码。Cursor 的 rules 功能可自定义 AI 行为,通过这些规则能提高动作建议的准确性。 Stagehand 的 Cursor 规则请查看 此文件 若使用 Windsurf,可将相同规则添加至 .windsurfrules 文件。

避免向 LLM 发送敏感信息

可在 act 调用中使用 variables 来避免向 LLM 发送敏感信息。
await page.act({
	action: "Type %email% into the email field",
	variables: {
		email: "[email protected]",
	},
});

执行前预览动作

使用 observe() 可获取动作而不执行。 若对动作满意,可直接用 act() 运行而无需 LLM 推理。
const [topAction] = await page.observe("Click the quickstart link");

/** The action will map 1:1 with a Playwright action:
{
	description: "The quickstart link",
	method: "click",
	selector: "/html/body/div[1]/div[1]/a",
	arguments: [],
}
**/

// NO LLM INFERENCE on observe results
await page.act(topAction)
observe() 也可用于敏感信息场景,如下所示。
const [topAction] = await page.observe("Type %email% into the email field");

/** The observe result will be an object with the following shape:
{
	description: "The email input field",
	method: "type",
	selector: "/html/body/div[1]/div[1]/input",
	arguments: ["%email%"],
}
*/

await page.act({
	...topAction,
	// This prevents LLMs from seeing sensitive information
	// No LLM inference is taken on observe results
	arguments: [sensitiveEmail],
})
使用 observe() 从当前页面获取可执行建议
const actions = await page.observe();
console.log("Possible actions:", actions);

// You can also use `observe()` with a custom prompt
const buttons = await page.observe({
	instruction: "find all the buttons on the page",
});

避免宽泛或模糊的指令

避免使用与当前页面无关或试图同时完成多项任务的指令。
// Too vague
await page.act({ action: "find something interesting on the page" });

// Avoid combining actions
await page.act({ action: "fill out the form and submit it" });

使用 observe() 获取完整表单值

const formValues = await page.observe("get the text inputs on the page");
该方法将返回包含以下结构的对象数组:
{
	description: "The text input field",
	method: "type",
	selector: "/html/body/div[1]/div[1]/input",
	arguments: ["some text"],
}
您可以直接在代码中使用这些操作。
for (const formValue of formValues) {
	await page.act({
		...formValue,
		arguments: [yourValueHere],
	});
}
完整示例请查看 Stagehand 代码库中的示例文件