diff --git a/.changeset/free-pots-move.md b/.changeset/free-pots-move.md new file mode 100644 index 000000000..459daf48c --- /dev/null +++ b/.changeset/free-pots-move.md @@ -0,0 +1,5 @@ +--- +"@browserbasehq/stagehand": patch +--- + +Added CDP support for screenshots, find more about the benefits here: https://docs.browserbase.com/features/screenshots#why-use-cdp-for-screenshots%3F diff --git a/lib/StagehandPage.ts b/lib/StagehandPage.ts index f7f211cbf..73064ba5a 100644 --- a/lib/StagehandPage.ts +++ b/lib/StagehandPage.ts @@ -251,6 +251,41 @@ export class StagehandPage { }; } + // Handle screenshots with CDP + if (prop === "screenshot") { + return async ( + options: { + type?: "png" | "jpeg"; + quality?: number; + fullPage?: boolean; + clip?: { x: number; y: number; width: number; height: number }; + omitBackground?: boolean; + } = {}, + ) => { + const cdpOptions: Record = { + format: options.type === "jpeg" ? "jpeg" : "png", + quality: options.quality, + clip: options.clip, + omitBackground: options.omitBackground, + fromSurface: true, + }; + + if (options.fullPage) { + cdpOptions.captureBeyondViewport = true; + } + + const data = await this.sendCDP<{ data: string }>( + "Page.captureScreenshot", + cdpOptions, + ); + + // Convert base64 to buffer + const buffer = Buffer.from(data.data, "base64"); + + return buffer; + }; + } + // Handle goto specially if (prop === "goto") { return async (url: string, options: GotoOptions) => { diff --git a/stagehand.config.ts b/stagehand.config.ts index 253608a6f..635c9f5cc 100644 --- a/stagehand.config.ts +++ b/stagehand.config.ts @@ -10,7 +10,7 @@ const StagehandConfig: ConstructorParams = { : "LOCAL", apiKey: process.env.BROWSERBASE_API_KEY /* API key for authentication */, projectId: process.env.BROWSERBASE_PROJECT_ID /* Project identifier */, - debugDom: true /* Enable DOM debugging features */, + debugDom: false /* Enable DOM debugging features */, headless: false /* Run browser in headless mode */, logger: (message: LogLine) => console.log(logLineToString(message)) /* Custom logging function */,