add --selector flag to scroll command (#537)

* add --selector flag to scroll command

The `scroll` command uses `window.scrollBy()`, which has no effect on apps
that use custom scrollable containers (e.g. a nested div with overflow-y: auto).

The backend `handleScroll` already supports a `selector` parameter, but the CLI
never exposed it. This adds `-s` / `--selector` to the `scroll` command so users
can target a specific scrollable element:

    agent-browser scroll down 500 --selector "div.scroll-container"

Also fixes the backend to apply `direction`/`amount` when a selector is present
(previously those fields were only used in the no-selector branch).

Closes #501

* fixes
This commit is contained in:
Chris Tate
2026-02-24 07:40:46 -06:00
committed by GitHub
parent 77f2caa1bc
commit f319195974
6 changed files with 157 additions and 35 deletions
+25 -25
View File
@@ -882,41 +882,41 @@ async function handleWait(command: WaitCommand, browser: BrowserManager): Promis
async function handleScroll(command: ScrollCommand, browser: BrowserManager): Promise<Response> {
const page = browser.getPage();
let deltaX = command.x ?? 0;
let deltaY = command.y ?? 0;
const hasExplicitDelta = command.x !== undefined || command.y !== undefined;
if (command.direction) {
const amount = command.amount ?? 100;
switch (command.direction) {
case 'up':
deltaY = -amount;
break;
case 'down':
deltaY = amount;
break;
case 'left':
deltaX = -amount;
break;
case 'right':
deltaX = amount;
break;
}
}
if (command.selector) {
const element = browser.getLocator(command.selector);
await element.scrollIntoViewIfNeeded();
if (command.x !== undefined || command.y !== undefined) {
if (hasExplicitDelta || deltaX !== 0 || deltaY !== 0) {
await element.evaluate(
(el, { x, y }) => {
el.scrollBy(x ?? 0, y ?? 0);
el.scrollBy(x, y);
},
{ x: command.x, y: command.y }
{ x: deltaX, y: deltaY }
);
}
} else {
// Scroll the page
let deltaX = command.x ?? 0;
let deltaY = command.y ?? 0;
if (command.direction) {
const amount = command.amount ?? 100;
switch (command.direction) {
case 'up':
deltaY = -amount;
break;
case 'down':
deltaY = amount;
break;
case 'left':
deltaX = -amount;
break;
case 'right':
deltaX = amount;
break;
}
}
await page.evaluate(`window.scrollBy(${deltaX}, ${deltaY})`);
}