Skip to content
Open
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
16 changes: 4 additions & 12 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1185,19 +1185,11 @@ async function executeCommand(
}
case "select": {
const [selector, values] = args as [string, string[]];
if (!stagehand) {
throw new Error("Stagehand instance not available");
}
const resolved = resolveSelector(selector);
// selectOption takes the first value as argument
const action = {
selector: resolved,
description: "select option",
method: "selectOption",
arguments: [values[0] || ""],
};
await stagehand.act(action);
return { selected: values };
const selected = await page!
.deepLocator(resolved)
.selectOption(values.length === 1 ? values[0] || "" : values);
return { selected };
}
case "upload": {
const [selector, filePaths] = args as [string, string[]];
Expand Down
16 changes: 16 additions & 0 deletions packages/cli/tests/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,22 @@ describe("Browse CLI", () => {
expect(data.typed).toBe(true);
});

it("should select multiple values in a multi-select element", async () => {
const html = `<!doctype html><html><body><select multiple><option value="red">Red</option><option value="blue">Blue</option><option value="green">Green</option></select></body></html>`;
const dataUrl = `data:text/html,${encodeURIComponent(html)}`;

await browse(`open "${dataUrl}"`);
const result = await browse("select select red blue");
const data = parseJson<{ selected: string[] }>(result.stdout);
expect(data.selected).toEqual(["red", "blue"]);

const selectedResult = await browse(
'eval "Array.from(document.querySelector(\\"select\\").selectedOptions).map(o=>o.value).join(\\\",\\\")"',
);
const selectedData = parseJson<{ result: string }>(selectedResult.stdout);
expect(selectedData.result).toBe("red,blue");
});

it("should press keys", async () => {
await browse("open https://example.com");
const result = await browse("press Tab");
Expand Down
6 changes: 4 additions & 2 deletions packages/core/lib/v3/handlers/handlerUtils/actHandlerUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,10 @@ const METHOD_HANDLER_MAP: Record<
export async function selectOption(ctx: UnderstudyMethodHandlerContext) {
const { locator, xpath, args } = ctx;
try {
const text = args[0]?.toString() || "";
await locator.selectOption(text);
const values = args.map((arg) => arg.toString());
await locator.selectOption(
values.length <= 1 ? (values[0] ?? "") : values,
);
} catch (e) {
const msg = e instanceof Error ? e.message : String(e);
const stack = e instanceof Error ? e.stack : undefined;
Expand Down
33 changes: 33 additions & 0 deletions packages/core/tests/integration/perform-understudy-method.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,39 @@ test.describe("tests performUnderstudyMethod", () => {
expect(inputValue).toBe("Smog Check Technician");
});

test("tests selecting multiple options from a multi-select dropdown", async () => {
const page = v3.context.pages()[0];
await page.goto(
"data:text/html," +
encodeURIComponent(
`<!doctype html><html><body>
<select id="colors" multiple>
<option value="red">Red</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
</select>
</body></html>`,
),
);

await performUnderstudyMethod(
page,
page.mainFrame(),
"selectOptionFromDropdown",
"xpath=//*[@id='colors']",
["red", "blue"],
30000,
);

const selectedValues = await page.evaluate(() => {
const select = document.getElementById("colors") as
| HTMLSelectElement
| null;
return Array.from(select?.selectedOptions ?? []).map((o) => o.value);
});
expect(selectedValues).toEqual(["red", "blue"]);
});

test("tests drag & drop works (start xpath & end xpath)", async () => {
const page = v3.context.pages()[0];
await page.goto(
Expand Down
Loading