How to Automate a Browser Task with Browser Use and a Python Script

November 9, 2026

Browser Use is a Python library that gives an LLM hands on a real browser. You describe a task — “find the price of the pro plan on this page and return it” — and the agent takes screenshots, extracts the page as an accessibility tree, clicks, types, and returns an answer.

It is a different tool from Playwright, and the difference matters. Playwright needs you to know the selector. Browser Use needs you to describe the outcome and tolerates the page changing.

What does Browser Use actually do under the hood?

Each step: capture the current page state, extract interactive elements with an indexed list, send that plus the task history to an LLM, receive an action, execute it. The loop repeats until the model reports done or hits the step limit.

The model never sees raw HTML by default — it sees a simplified tree, which is why it survives redesigns that would break a CSS selector. It also means the model cannot see things that are not in that tree, such as content inside a canvas or behind an iframe it was not told about.

Who this is for: workflows on third-party sites with no API, research tasks across a handful of pages, and internal tools where writing twenty selectors is not worth it. Who it is not for: high-volume scraping (use an API or a scraper), or anything where a wrong click costs money and you cannot tolerate variance.

How do you install and run the first agent?

The library needs a browser it can drive. Chromium is bundled through Playwright:

pip install browser-use
playwright install chromium

Then a minimal agent:

from browser_use import Agent
from langchain_openai import ChatOpenAI
import asyncio

agent = Agent(
    task="Go to news.ycombinator.com and return the title of the top post",
    llm=ChatOpenAI(model="gpt-4o-mini"),
)
result = asyncio.run(agent.run())
print(result)

Two things to note. The LLM must support vision, because screenshots are part of the state. And the model choice dominates both cost and success rate: a small model is cheap per step but takes more steps and fails more.

Why does the first run cost more than expected?

Because every step sends the full page state plus history to the model. A ten-step task with a 4,000-token tree per step is 40,000 tokens of input, and that repeats with growing history.

Three fixes, in order of impact:

  • Use vision selectively. Screenshots are expensive in tokens. Turning them off for text-heavy pages and on for layout-dependent ones cuts input significantly. – Cap max_steps. Ten is generous for most extraction tasks. A runaway agent will happily take sixty steps. – Start the agent on the right page. Navigate to the URL in code, then let the agent work. Making the agent “find a site” wastes steps on search results.

What breaks in real runs?

Three failure modes cover most of what you will see:

  1. Cookies and consent banners. A GDPR modal eats the first three steps and blocks clicks. Either pre-dismiss it with a short Playwright snippet before handing control to the agent, or include the instruction “dismiss any cookie banner first” in the task. 2. Login walls. The agent cannot log in unless it has credentials and can reach the form. Supply credentials through the browser profile, not in the task string, and never paste passwords into a prompt you log. 3. Index shifting. The element list is regenerated each step, so “click element 12” refers to the current state only. This is handled internally, but any custom action you write must re-read the state rather than cache indices.

How do you make it deterministic enough for production?

You cannot make an LLM-driven browser fully deterministic, so structure the job instead:

  • Assert the output shape. If the task is “return a price”, validate that the returned string parses as a number before doing anything with it. – Retry the whole run, not the step. A failed run usually failed for a reason present from step one. – Log the action trace. Browser Use records each action; keep those logs, because “it returned the wrong price” is undebuggable without them. – Set a step and time budget. A run that exceeds 60 seconds or 12 steps should abort and report failure, not keep trying.

For anything you run more than a few times a day, record the successful action sequence once and replay it with Playwright. That turns a 4,000-token-per-step agent into a free script, with the agent kept around for when the site changes.

What does it cost per run?

A ten-step run on a small model is typically a few cents; on a frontier vision model it can be ten times that. At 200 runs a day the difference between model tiers is the difference between a coffee budget and a real line item.

Run the same task set across two model tiers and compare success rate, not cost alone. A model that costs 3x and succeeds 90% versus 60% is cheaper per successful run.

When should you not use an agent at all?

If the site has an API, use it. If the page structure is stable and you scrape it daily, a selector-based scraper is faster, free, and does not get confused by a redesign of the header. If the task needs to run at 100 requests per second, this is the wrong architecture entirely.

Browser Use earns its place where the site has no API, the layout changes, and the volume is low.

Quick Summary

  • Browser Use drives a real Chromium via Playwright and gives the LLM an indexed element tree plus screenshots, so it survives layout changes.

  • Install with pip install browser-use and playwright install chromium; the model must support vision.

  • Cost is dominated by per-step page state, so cap max_steps, navigate to the URL in code, and use vision only where needed.

  • Cookie banners and login walls cause most failures; pre-dismiss them or supply a browser profile with credentials.

  • For repeated jobs, record the successful action sequence once and replay it with Playwright.