Skip to content

connect to a running chrome locally #547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions examples/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@
* npx create-browser-app@latest my-browser-app
*/

import { Stagehand } from "@/dist";
import StagehandConfig from "@/stagehand.config";
import { Stagehand } from "@/lib/index";

async function example() {
const stagehand = new Stagehand({
...StagehandConfig,
env: "LOCAL",
localDebugPort: 9222,
});
await stagehand.init();
await stagehand.page.goto("https://docs.stagehand.dev");
const page = stagehand.page;
await page.act(
`type 'if you're seeing this tweet, get excited -- i'm using stagehand on arc!'`,
);
await page.act("click the post button");
await stagehand.close();
}

(async () => {
Expand Down
19 changes: 18 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ async function getBrowser(
browserbaseSessionCreateParams?: Browserbase.Sessions.SessionCreateParams,
browserbaseSessionID?: string,
localBrowserLaunchOptions?: LocalBrowserLaunchOptions,
localDebugPort?: number,
): Promise<BrowserResult> {
if (env === "BROWSERBASE") {
if (!apiKey) {
Expand Down Expand Up @@ -200,6 +201,14 @@ async function getBrowser(
const context = browser.contexts()[0];

return { browser, context, debugUrl, sessionUrl, sessionId, env };
} else if (localDebugPort) {
const browser = await chromium.connectOverCDP(
`http://localhost:${localDebugPort}`,
);
const context = browser.contexts()[0];
await applyStealthScripts(context);

return { context, contextPath: undefined, env: "LOCAL" };
} else {
logger({
category: "init",
Expand Down Expand Up @@ -377,6 +386,7 @@ export class Stagehand {
private waitForCaptchaSolves: boolean;
private localBrowserLaunchOptions?: LocalBrowserLaunchOptions;
public readonly selfHeal: boolean;
private readonly localDebugPort?: number;
private cleanupCalled = false;

constructor(
Expand All @@ -399,6 +409,7 @@ export class Stagehand {
systemPrompt,
useAPI,
localBrowserLaunchOptions,
localDebugPort,
selfHeal = true,
waitForCaptchaSolves = false,
}: ConstructorParams = {
Expand Down Expand Up @@ -437,6 +448,7 @@ export class Stagehand {
this.userProvidedInstructions = systemPrompt;
this.usingAPI = useAPI ?? false;
this.modelName = modelName ?? DEFAULT_MODEL_NAME;
this.localDebugPort = localDebugPort;

if (this.usingAPI && env === "LOCAL") {
throw new Error("API mode can only be used with BROWSERBASE environment");
Expand Down Expand Up @@ -559,6 +571,7 @@ export class Stagehand {
this.browserbaseSessionCreateParams,
this.browserbaseSessionID,
this.localBrowserLaunchOptions,
this.localDebugPort,
).catch((e) => {
console.error("Error in init:", e);
const br: BrowserResult = {
Expand All @@ -573,7 +586,11 @@ export class Stagehand {
this.intEnv = env;
this.contextPath = contextPath;
this.stagehandContext = await StagehandContext.init(context, this);
const defaultPage = this.context.pages()[0];
const pages = this.context.pages();
let defaultPage = pages[0];
if (!defaultPage) {
defaultPage = await this.context.newPage();
}
this.stagehandPage = await new StagehandPage(
defaultPage,
this,
Expand Down
6 changes: 6 additions & 0 deletions types/stagehand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ export interface ConstructorParams {
*/
waitForCaptchaSolves?: boolean;
localBrowserLaunchOptions?: LocalBrowserLaunchOptions;
/**
* The port to use for local debugging. Do not set this if you are using localBrowserLaunchOptions.
*
* @default 9222
*/
localDebugPort?: number;
}

export interface InitOptions {
Expand Down