DEV Community

Isaias Velasquez
Isaias Velasquez

Posted on

[BA-004] browser-use: let an AI agent drive the browser for you

In BA-003 we built a manual agent loop. There is an open source library that does this for you called browser-use.

browser-use is a Python library that connects an LLM to a Playwright controlled browser. You give it a task in plain language and it figures out the steps.

Install it:

pip install browser-use
playwright install
Enter fullscreen mode Exit fullscreen mode

The core API is a single Agent class. Here is the simplest example:

from langchain_openai import ChatOpenAI
from browser_use import Agent

llm = ChatOpenAI(model="gpt-4o")

agent = Agent(
    task="Go to example.com and tell me the page title",
    llm=llm
)

result = await agent.run()
print(result)
Enter fullscreen mode Exit fullscreen mode

The agent opens a browser, navigates to the page, reads the title and returns it. All the observation-action loop from BA-003 is handled internally.

You can also watch it work by passing a parameter:

agent = Agent(
    task="Search Google for 'browser-use python' and open the first result",
    llm=llm,
    use_vision=True
)

agent = Agent(
    task="Go to github.com, find the browser-use repository and tell me how many stars it has",
    llm=llm,
    use_vision=True
)
Enter fullscreen mode Exit fullscreen mode

With use_vision=True the agent takes screenshots and uses the LLM vision capabilities to understand the page layout. Without it, the agent uses the accessibility tree (see BA-001).

For longer running tasks you can use a context manager:

from browser_use import Agent

async def main():
    agent = Agent(
        task="Log in to example.com with username admin and password secret, "
             "then download the report and save it",
        llm=llm
    )
    async with agent:
        result = await agent.run(max_steps=30)
    print(result)
Enter fullscreen mode Exit fullscreen mode

The max_steps parameter limits how many actions the agent can take. This prevents infinite loops.

Browser-use also lets you register custom actions:

from browser_use import Agent, ActionResult

def custom_extract(page):
    data = page.locator(".data-table").inner_text()
    return ActionResult(extracted_content=data)

agent = Agent(
    task="Extract the table from the page",
    llm=llm,
    controller=custom_extract
)
Enter fullscreen mode Exit fullscreen mode

This is useful when the built in tools are not enough for your specific use case.

The library works with any LangChain compatible LLM, including OpenAI, Anthropic, Ollama and local models. You can run it headless or with a visible browser for debugging.

Internally browser-use is built on top of Playwright (BA-002) and uses the same CDP protocol (BA-001) under the hood. The library handles all the complexity: element detection, waiting, scrolling, error recovery and history management.

Browser-use is the fastest way to turn a natural language task into a working browser automation script. Try it with a simple task first and then scale up.

That's all for now.
Thanks for reading!

Top comments (0)