Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/yellow-mails-deny.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@browserbasehq/stagehand": patch
---

fix issue where locator.fill() was not working on elements that require direct value setting
147 changes: 147 additions & 0 deletions packages/core/lib/v3/tests/locator-fill.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import { expect, test } from "@playwright/test";
import { V3 } from "../v3";
import { StagehandLocatorError } from "../types/public/sdkErrors";
import { v3TestConfig } from "./v3.config";

test.describe("Locator.fill()", () => {
let v3: V3;

test.beforeEach(async () => {
v3 = new V3(v3TestConfig);
await v3.init();
});

test.afterEach(async () => {
await v3?.close?.().catch((e) => {
void e;
});
});

test("fills date inputs via value setter even when beforeinput blocks insertText", async () => {
const page = v3.context.pages()[0];

await page.goto(
"data:text/html," +
encodeURIComponent(
`<!doctype html><html><body>
<input id="date" type="date" />
<script>
const input = document.getElementById('date');
input.addEventListener('beforeinput', (e) => {
if (e && e.inputType === 'insertText') e.preventDefault();
});
</script>
</body></html>`,
),
);

const dateInput = page.mainFrame().locator("xpath=/html/body/input");
await dateInput.fill("2026-01-01");

const value = await dateInput.inputValue();
expect(value).toBe("2026-01-01");
});

test("xpath case: throws StagehandLocatorError when fill encounters an exception", async () => {
const page = v3.context.pages()[0];

await page.goto(
"data:text/html," +
encodeURIComponent(
`<!doctype html><html><body>
<input id="date" type="date" />
</body></html>`,
),
);

await page.waitForSelector("xpath=/html/body/input");

await page.evaluate(() => {
const input = document.querySelector("input");
Object.defineProperty(input, "isConnected", {
get() {
throw new Error("boom");
},
});
});

const dateInput = page.mainFrame().locator("xpath=/html/body/input");
let error: unknown;
try {
await dateInput.fill("2026-01-01");
} catch (err) {
error = err;
}

expect(error).toBeInstanceOf(StagehandLocatorError);
if (error instanceof Error) {
// Log the message so it's visible in test output.
expect(error.message).toContain("Error Filling Element");
expect(error.message).toContain("selector: xpath=/html/body/input");
expect(error.message).toContain("boom");
}
});

test("css selector case: throws StagehandLocatorError when fill encounters an exception", async () => {
const page = v3.context.pages()[0];

await page.goto(
"data:text/html," +
encodeURIComponent(
`<!doctype html><html><body>
<input id="date" type="date" />
</body></html>`,
),
);

await page.waitForSelector("#date");

// Override in main world
await page.evaluate(() => {
const input = document.querySelector("input");
Object.defineProperty(input, "isConnected", {
get() {
throw new Error("boom");
},
configurable: true,
});
});

// Also override in the isolated world that CSS selectors use
const frameId = page.mainFrameId();
const { executionContextId } = await page.sendCDP<{
executionContextId: number;
}>("Page.createIsolatedWorld", {
frameId,
worldName: "v3-world",
});

await page.sendCDP("Runtime.evaluate", {
expression: `(() => {
const input = document.querySelector('input');
if (input) {
Object.defineProperty(input, 'isConnected', {
get() { throw new Error("boom"); },
configurable: true
});
}
})()`,
contextId: executionContextId,
});

const dateInput = page.mainFrame().locator("#date");
let error: unknown;
try {
await dateInput.fill("2026-01-01");
} catch (err) {
error = err;
}

expect(error).toBeInstanceOf(StagehandLocatorError);
if (error instanceof Error) {
expect(error.message).toContain("Error Filling Element");
expect(error.message).toContain("selector: #date");
expect(error.message).toContain("boom");
}
});
});
8 changes: 8 additions & 0 deletions packages/core/lib/v3/types/public/sdkErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ export class StagehandDomProcessError extends StagehandError {
}
}

export class StagehandLocatorError extends StagehandError {
constructor(action: string, selector: string, message: string) {
super(
`Error ${action} Element with selector: ${selector} Reason: ${message}`,
);
}
}

export class StagehandClickError extends StagehandError {
constructor(message: string, selector: string) {
super(
Expand Down
19 changes: 17 additions & 2 deletions packages/core/lib/v3/understudy/locator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ import { Protocol } from "devtools-protocol";
import * as fs from "fs";
import * as os from "os";
import * as path from "path";
import { locatorScriptSources } from "../dom/build/locatorScripts.generated";
import {
locatorScriptBootstrap,
locatorScriptGlobalRefs,
locatorScriptSources,
} from "../dom/build/locatorScripts.generated";
import type { Frame } from "./frame";
import { FrameSelectorResolver, type SelectorQuery } from "./selectorResolver";
import {
StagehandElementNotFoundError,
StagehandInvalidArgumentError,
StagehandLocatorError,
ElementNotVisibleError,
} from "../types/public/sdkErrors";
import { normalizeInputFiles } from "./fileUploadUtils";
Expand Down Expand Up @@ -510,6 +515,8 @@ export class Locator {
*/
async fill(value: string): Promise<void> {
const session = this.frame.session;
// Use the bundled locator globals; the raw fill snippet depends on helper symbols.
const fillDeclaration = `function(value) { ${locatorScriptBootstrap}; return ${locatorScriptGlobalRefs.fillElementValue}.call(this, value); }`;
const { objectId } = await this.resolveNode();

let releaseNeeded = true;
Expand All @@ -519,11 +526,19 @@ export class Locator {
"Runtime.callFunctionOn",
{
objectId,
functionDeclaration: locatorScriptSources.fillElementValue,
functionDeclaration: fillDeclaration,
arguments: [{ value }],
returnByValue: true,
},
);
if (res.exceptionDetails) {
// prefer exception.description over text (eg "Uncaught")
const message =
res.exceptionDetails.exception?.description ??
res.exceptionDetails.text ??
"Unknown exception during locator().fill()";
throw new StagehandLocatorError("Filling", this.selector, message);
}

const result = res.result.value as
| { status?: string; reason?: string; value?: string }
Expand Down
1 change: 1 addition & 0 deletions packages/core/tests/public-api/public-error-types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const publicErrorTypes = {
StagehandIframeError: Stagehand.StagehandIframeError,
StagehandInitError: Stagehand.StagehandInitError,
StagehandInvalidArgumentError: Stagehand.StagehandInvalidArgumentError,
StagehandLocatorError: Stagehand.StagehandLocatorError,
StagehandMissingArgumentError: Stagehand.StagehandMissingArgumentError,
StagehandNotInitializedError: Stagehand.StagehandNotInitializedError,
StagehandResponseBodyError: Stagehand.StagehandResponseBodyError,
Expand Down
Loading