# browser-use

> Give a browser-use agent two custom tools that find a captcha, solve it with NoptCHA and put the token back - for agents running a local browser.

Source: https://noptcha.com/docs/integrations/browser-use/

[browser-use](https://docs.browser-use.com/) has no plug-in point for a captcha service; it has [custom tools](https://docs.browser-use.com/open-source/tools). In browser-use 1.0 a tool receives only the arguments the model passes, and the model drives the browser itself from Python, running JavaScript in the page with `js(...)`. So the two tools below hand the model JavaScript to run:

-   `find_captcha_script` returns the [helper module](https://noptcha.com/docs/integrations.md#the-helper-modules)’s `FIND_JS` as an expression. The model runs it with `js(...)` and gets `[{"type", "params"}]`.
-   `solve_captcha(task_type, params)` solves the task with the v1 API (your key stays in your process) and returns an expression that puts the token into the page. The model runs it, then submits the form.

This is for agents that run a **local browser** (`Browser("chromium")`, `Browser("chrome")` or `Browser(cdp_url=…)`). The browser-use cloud browsers handle captchas on their own.

## Setup

```sh
pip install browser-use requests
export NOPTCHA_KEY=nop_test_...   # a test key: free, fixed answers
# no account yet? export NOPTCHA_KEY=keyless (keyless sandbox, same fixed answers)
export ANTHROPIC_API_KEY=...      # or the key of the model you use
```

Save [`noptcha_browser.py`](https://noptcha.com/docs/integrations.md#the-helper-modules) next to your script. `Browser("chromium")` uses an installed Chrome, Chromium or Edge, else the Chromium that `browser-use install` downloads.

## Example

agent.py

```python
import asyncio
import json

from browser_use import Agent, Browser, ChatAnthropic, Tools

from noptcha_browser import FIND_JS, PUT_JS, solve

tools = Tools()


@tools.action(
    "Get JavaScript that lists the captchas on the current page. Run it with js(...); each result has a type and "
    "params to pass to solve_captcha."
)
def find_captcha_script() -> str:
    return f"({FIND_JS})()"


@tools.action(
    "Solve a captcha found by find_captcha_script with Noptcha. Returns JavaScript that puts the token into the "
    "page; run it with js(...), then submit the form."
)
def solve_captcha(task_type: str, params: dict) -> str:
    solution = solve(task_type, params)  # blocks until solved; raises CaptchaError if the task failed
    return f"({PUT_JS})({json.dumps({'type': task_type, 'token': solution['token']})})"


async def main():
    agent = Agent(
        "Open https://example.com/signup and sign up with test@example.com. "
        "If a captcha is on the page, use find_captcha_script and solve_captcha before you submit.",
        llm=ChatAnthropic("claude-sonnet-5-5"),  # any model browser-use supports
        browser=Browser("chromium", headless=True),  # a local browser
        tools=tools,
    )
    result = await agent.run()
    print(result.final_result())


if __name__ == "__main__":
    asyncio.run(main())
```

The task text tells the model that the tools exist and when to use them; the tool descriptions tell it what to do with the results. You can also put that into `extend_system_message=` once instead of every task.

## Notes

-   **Errors.** If the task fails, `solve()` raises `CaptchaError` (for example `unsolvable`); browser-use hands the error to the model, which can retry or stop. Failed tasks are not charged.
-   **Budget.** Give the agent a key with a spending limit and the task types it needs (see [Keys and authentication](https://noptcha.com/docs/authentication.md)): the model decides how often to call the tool.
-   **Without the agent.** The same JavaScript works in any code that drives the page: `js(find_captcha_script())` in the agent’s REPL, `page.evaluate(...)` in Playwright.

> **Note:** Tested with browser-use 1.0.8: the tools’ JavaScript was run against local pages that imitate each widget, with a test key, and the agent was built as in the example. Whether a model calls the tools when it should depends on the model and the task text.
