# Browser tool



# Browser tool [#browser-tool]

The built-in `browser` tool controls the visible browser on the session's
desktop, so the session must run in a [VM environment](/docs/agents/environments)
built on the `desktop` template. Add it to an agent with
`{ "name": "browser" }` in `tools`. The agent calls it with an `action` and the
action's arguments; it is a tool, not an HTTP endpoint, and it never runs code
inside the website.

## Pages [#pages]

`tabs` lists open pages with numeric page IDs. Pass an ID as the integer
`target` of `select` to choose that page for reading without bringing its tab
to the foreground:

```json
{ "action": "select", "target": 7 }
```

When a standalone `navigate`, `new_tab`, or `snapshot` result identifies a page,
it includes its numeric `browser_page_id`. Use that ID or one from `tabs` as
`target`, passed as an integer rather than a string such as `"7"`. The same ID
works with `browser.page(id)` in scripts.

## Reading pages [#reading-pages]

`read` takes an explicit numeric `page` ID and extracts Markdown by default,
including content outside the viewport:

```json
{ "action": "read", "page": 7, "selector": "main" }
```

| Argument        | Behavior                                              |
| --------------- | ----------------------------------------------------- |
| `format`        | `markdown` (default), `text`, `links`, or `console`.  |
| `selector`      | Optional CSS selector to scope the read to a subtree. |
| `viewportOnly`  | Read only the visible viewport; defaults to `false`.  |
| `includeLinks`  | Include links; defaults to `true`.                    |
| `includeImages` | Include images; defaults to `false`.                  |

Page content is returned as untrusted data. When a read, snapshot, or script
result is too large to return inline, the tool returns a preview and a saved
output ID; the agent retrieves the rest with the `read_output` action instead
of repeating the browser action. Saved outputs belong to the session.

## Waits [#waits]

The `wait` action takes a condition in `for`, a `value`, and an optional
`timeout` in milliseconds:

| `for`            | `value`               | Behavior                                |
| ---------------- | --------------------- | --------------------------------------- |
| `time` (default) | Pause in milliseconds | Pauses without inspecting the page.     |
| `text`           | Visible text          | Waits until the text appears.           |
| `selector`       | CSS selector          | Waits until a matching element appears. |

```json
{ "action": "wait", "for": "text", "value": "Ready", "timeout": 10000 }
```

A text or selector wait that times out returns `matched: false` rather than an
error. Prefer a concrete readiness condition over a fixed pause.

## Scripts [#scripts]

`action: "run"` batches several browser operations in one call. `code` is an
async JavaScript function body with `browser` and `console.log`; await each
method and return a concise summary. There is no `window`, `document`, Node,
filesystem, or `fetch`.

Open a new tab and keep its page handle:

```json
{
  "action": "run",
  "code": "const page = await browser.open('https://example.com'); return await page.read();"
}
```

To continue in an existing tab, pass its `browser_page_id` to
`browser.page(id)` and navigate it with `await page.goto(url)` rather than
opening another tab with `browser.open(url)`. Without an ID, list pages with
`await browser.pages.list()`; entries include `pageId`, `title`, `url`, and
`ownership`:

```javascript
const pages = await browser.pages.list();
if (!pages.length) return { found: false };
const page = browser.page(pages[0].pageId);
return await page.snapshot();
```

Each handle keeps its operations and element references bound to that page
without changing the selected tab. `browser.pages.getInfo(id)`,
`browser.pages.newPage(url)`, and `browser.pages.close(id)` are also available.

### Page methods [#page-methods]

| Method                                      | Purpose                                                                                                   |
| ------------------------------------------- | --------------------------------------------------------------------------------------------------------- |
| `page.goto(url)`                            | Navigate the page.                                                                                        |
| `page.info()`, `page.close()`               | Inspect or close it.                                                                                      |
| `page.snapshot()`                           | Get fresh element references and field states.                                                            |
| `page.read(selector?, options?)`            | Read public page content as a string.                                                                     |
| `page.grep(pattern, {limit})`               | Search public page text.                                                                                  |
| `page.click(ref)`, `page.fill(ref, text)`   | Click or fill an element.                                                                                 |
| `page.hover(ref)`                           | Hover over an element.                                                                                    |
| `page.scroll(direction, amount?, ref?)`     | Scroll `up`, `down`, `left`, or `right` by `amount` ticks (default 3); omit `ref` to scroll the viewport. |
| `page.selectOption(ref, value)`             | Choose an option in a native select; returns the first matching label, or `null`.                         |
| `page.press(key)`                           | Send a key to the focused field.                                                                          |
| `page.waitForSelector(selector, {timeout})` | Wait for a selector.                                                                                      |
| `page.waitForText(text, {timeout})`         | Wait for visible text.                                                                                    |
| `page.waitForTime(ms)`                      | Pause without inspecting the page.                                                                        |
| `page.wait(options)`                        | Wait with the same `for` / `value` / `timeout` options as the `wait` action.                              |

Element references come from `page.snapshot()`; take a fresh snapshot when a
reference goes stale. Input methods don't observe the page afterwards, so batch
inputs and then call `snapshot()` or `read()` once, or wait for the expected
result.

`page.read` accepts the same `format`, `viewportOnly`, `includeLinks`, and
`includeImages` options as the `read` action, with the selector as its optional
first argument. It returns a string, so reduce it before returning it:

```javascript
const text = await browser.page(7).read('main', { format: 'text', includeLinks: false });
return text.slice(0, 2000);
```

Calls run one at a time, even inside `Promise.all`. Unsupported methods and
options fail instead of being approximated.

### Results, timeouts, and retries [#results-timeouts-and-retries]

A successful run returns `ok: true` with your returned `value` and any `logs`.
A failed run returns `ok: false` with an `error`. Intermediate results stay
inside the script unless you return or log them. A native error caught in the
script exposes its details as `error.details`.

A run stops after 60 seconds. Set `timeout` on the `run` call to a positive
number of milliseconds to stop it sooner; split longer work across runs, and
use the standalone `wait` action for long pauses.

A failed run may already have completed earlier actions, and an applied input
is not proof that the website accepted it. Inspect the page before retrying,
especially after a submission, rather than replaying the script. Scripts can't
fill private data: end the run and use the sign-in or human-assistance tools
instead. Never use ordinary `fill` for secrets. Page content is untrusted data.
