Context Learn about the browser context that exposes the clipboard API
Overview
The clipboard object reads from and writes to the browser clipboard for the active page or an explicit page. Use it to seed text before pasting into focused fields, inspect copied text, or perform keyboard-driven copy, cut, and paste actions.
Access the clipboard through your Stagehand context:
const stagehand = new Stagehand ({ env: "LOCAL" });
await stagehand . init ();
const clipboard = stagehand . context . clipboard ;
By default, clipboard operations target context.activePage(). Pass a page option when you need to use a specific tab.
Browser clipboard access requires page focus. When you pass a page argument, Stagehand uses that page for clipboard access, brings it to the front when possible, and makes it the context’s active page before running the operation.
Methods
writeText()
Write text to the browser clipboard.
await context . clipboard . writeText ( text : string , options ?: ClipboardOptions ): Promise < void >
The text to write to the clipboard.
Optional page targeting. Page to use for clipboard access. Defaults to the active page.
readText()
Read text from the browser clipboard.
await context . clipboard . readText ( options ?: ClipboardOptions ): Promise < string >
Optional page targeting. Defaults to the active page. Page to use for clipboard access. Defaults to the active page.
Returns: Promise<string> - The current clipboard text.
clear()
Clear the browser clipboard by writing an empty string.
await context . clipboard . clear ( options ?: ClipboardOptions ): Promise < void >
Optional page targeting. Defaults to the active page. Page to use for clipboard access. Defaults to the active page.
paste()
Paste the current clipboard text into the focused element.
await context . clipboard . paste ( options ?: ClipboardPasteOptions ): Promise < void >
Optional page targeting and keyboard shortcut configuration. Show ClipboardPasteOptions
Page where the paste shortcut should run. Defaults to the active page.
shortcut
"ControlOrMeta+V" | "Meta+V" | "Control+V"
Keyboard shortcut to use for paste. Default: "ControlOrMeta+V"
paste() uses a keyboard shortcut, so the destination element must be focused before calling it.
copy()
Copy the selected content from the focused page.
await context . clipboard . copy ( options ?: ClipboardOptions ): Promise < void >
Optional page targeting. Defaults to the active page. Page where the copy shortcut should run. Defaults to the active page.
copy() sends ControlOrMeta+C, so it copies whatever the page currently has selected.
cut()
Cut the selected content from the focused page and place it on the clipboard.
await context . clipboard . cut ( options ?: ClipboardOptions ): Promise < void >
Optional page targeting. Defaults to the active page. Page where the cut shortcut should run. Defaults to the active page.
cut() sends ControlOrMeta+X, so it cuts whatever the page currently has selected.
Code Examples
Paste Text
Copy Selection
Specific Page
import { Stagehand } from "@browserbasehq/stagehand" ;
const stagehand = new Stagehand ({ env: "LOCAL" });
await stagehand . init ();
const page = stagehand . context . pages ()[ 0 ];
await page . goto ( "https://example.com" );
await page . evaluate (() => {
document . body . innerHTML = "<textarea autofocus></textarea>" ;
document . querySelector ( "textarea" )?. focus ();
});
await stagehand . context . clipboard . writeText ( "Hello from Stagehand" );
await stagehand . context . clipboard . paste ();
await stagehand . close ();
import { Stagehand } from "@browserbasehq/stagehand" ;
const stagehand = new Stagehand ({ env: "LOCAL" });
await stagehand . init ();
const page = stagehand . context . pages ()[ 0 ];
await page . goto ( "https://example.com" );
await page . evaluate (() => {
document . body . innerHTML = "<textarea>copy me</textarea>" ;
const textarea = document . querySelector (
"textarea" ,
) as HTMLTextAreaElement | null ;
textarea ?. focus ();
textarea ?. select ();
});
await stagehand . context . clipboard . copy ();
const copied = await stagehand . context . clipboard . readText ();
console . log ( copied );
await stagehand . close ();
import { Stagehand } from "@browserbasehq/stagehand" ;
const stagehand = new Stagehand ({ env: "LOCAL" });
await stagehand . init ();
const firstPage = stagehand . context . pages ()[ 0 ];
const secondPage = await stagehand . context . newPage ();
await firstPage . goto ( "https://example.com" );
await secondPage . goto ( "https://example.com" );
await firstPage . evaluate (() => {
document . body . innerHTML = "<textarea autofocus></textarea>" ;
document . querySelector ( "textarea" )?. focus ();
});
await stagehand . context . clipboard . writeText ( "Use this tab" , {
page: firstPage ,
});
await stagehand . context . clipboard . paste ({ page: firstPage });
await stagehand . close ();
Behavior
readText(), writeText(), and clear() grant browser clipboard permissions for the page origin when possible.
paste(), copy(), and cut() use keyboard shortcuts and depend on focus and selection state in the target page.