> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.doc-detective.com/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.doc-detective.com/_mcp/server.

# runBrowserScript

> Reference for the `runBrowserScript` schema.

## Referenced In

* [Markup definition](/reference/schemas/markup-definition)
* [test](/reference/schemas/test)
* [Resolved context](/reference/schemas/resolved-context)

## Fields

| Field            | Type                                                                                                       | Description                                                                                                                                                                                                                                                                                                                                   | Default |
| :--------------- | :--------------------------------------------------------------------------------------------------------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :------ |
| runBrowserScript | one of: - string - object([Run browser script (detailed)](/reference/schemas/run-browser-script-detailed)) | Required. Execute arbitrary JavaScript in the browser page context. Runs via the WebDriver `executeScript` endpoint, so it has access to the page's `document`, `window`, and DOM. Doc Detective captures the script's return value in the step's `outputs.result`. Distinct from `runCode`, which runs Node/Python/bash on the host machine. | —       |

## Examples

```json
{
  "runBrowserScript": "return document.title;"
}
```

```json
{
  "runBrowserScript": {
    "script": "return document.title;"
  }
}
```

```json
{
  "runBrowserScript": {
    "script": "return arguments[0] + arguments[1];",
    "args": [
      "foo",
      "bar"
    ]
  }
}
```

```json
{
  "runBrowserScript": {
    "script": "return window.location.href;",
    "output": "/^https?:\\/\\//"
  }
}
```

```json
{
  "runBrowserScript": {
    "script": "return document.querySelectorAll('a').length;",
    "output": "5"
  }
}
```

```json
{
  "runBrowserScript": {
    "script": "return JSON.stringify({ ready: document.readyState });",
    "path": "ready-state.json",
    "directory": "output",
    "maxVariation": 0.1,
    "overwrite": "aboveVariation"
  }
}
```

```json
{
  "runBrowserScript": {
    "script": "localStorage.setItem('token', arguments[0]);",
    "args": [
      "$AUTH_TOKEN"
    ]
  }
}
```