Skip to content

将 Stagehand 集成到现有项目中。

文档索引

可在此获取完整文档索引:https://docs.stagehand.dev/llms.txt

在进一步浏览前,可使用该文件发现所有可用页面。

在当前应用中通过 TypeScript SDK 安装 Stagehand。

Terminal window
npm install @browserbasehq/stagehand

设置环境变量(或者通过你的框架使用 .env):

Terminal window
OPENAI_API_KEY=your_api_key
BROWSERBASE_API_KEY=your_api_key

在需要浏览器自动化的地方引入 Stagehand。

import dotenv from "dotenv";
import { Stagehand } from "@browserbasehq/stagehand";
import { z } from "zod";
dotenv.config({ path: ".env" }); // 如有需要
async function main() {
const stagehand = new Stagehand({
env: "BROWSERBASE"
});
await stagehand.init();
const page = stagehand.context.pages()[0];
await page.goto("https://example.com");
// 在页面上执行操作
await stagehand.act("Click the learn more button");
// 提取结构化数据
const description = await stagehand.extract("extract the description", z.string());
console.log(description);
await stagehand.close();
}
main().catch((err) => {
console.error(err);
process.exit(1);
});

Configuration

环境、Browserbase 与 Local 的差异、日志、超时以及 LLM 自定义。

查看原文

Extract

使用 Zod schema 提取强类型数据。

查看原文

-
0:000:00