A browser automation agent powered by Playwright and driven by Large Language Models (LLMs) via LangGraph and LiteLLM.
- Interactive chat mode for step-by-step browser control.
- Batch mode for executing predefined instruction sequences.
- Support for multiple LLM providers (OpenAI, Anthropic, Google, etc.) via LiteLLM.
- Uses Playwright's accessibility features via MCP for robust interaction.
- Optional screenshot recording to
artifacts/<timestamp>/step_<N>.pngfor visual traceability. - Headless browser support.
-
Prerequisites:
- Python 3.13+
- Node.js (for
npxto run the Playwright MCP server) uvPython package manager (pip install uv)
-
Clone the repository:
git clone <repository-url> cd playwright-browser-agent
-
Create virtual environment and install dependencies:
uv venv uv sync
-
Configure Environment Variables: The agent relies on environment variables for configuration, especially for selecting the LLM and providing API keys. These can be set directly in your shell or, more conveniently, placed in a
.envfile in the project root.Required Variables:
LLM_PROVIDER: Specifies the LLM provider to use (e.g.,openai,anthropic,google,ollama, etc.). Must match a provider supported by LiteLLM.LLM_MODEL: Specifies the exact model name for the chosen provider (e.g.,gpt-4o,claude-3-opus-20240229,gemini/gemini-1.5-pro-latest,llama3).- Provider API Key: You must set the corresponding API key environment variable for your chosen
LLM_PROVIDER. Common examples include:OPENAI_API_KEY(foropenaiprovider)ANTHROPIC_API_KEY(foranthropicprovider)GOOGLE_API_KEY(forgoogleprovider)- Refer to the LiteLLM documentation for the specific key names required by other providers.
Optional Variables:
- Variables corresponding to CLI flags (like
HEADLESS,RECORD) can also be set in.env, but CLI flags will always take precedence.
Create a
.envfile in the project root and add the required API keys for your chosen LLM provider(s). For example:# .env LLM_PROVIDER=openai LLM_MODEL=gpt-4o OPENAI_API_KEY=sk-... # Or for Anthropic: # LLM_PROVIDER=anthropic # LLM_MODEL=claude-3-sonnet-20240229 # ANTHROPIC_API_KEY=sk-ant-...
The agent will validate these environment variables at startup. If
LLM_PROVIDERorLLM_MODELare missing, or if the required API key for the selected provider is not found in the environment, the application will exit with an error message.
The agent provides two main commands: chat and batch.
Important: The Playwright MCP server (@playwright/mcp) is managed automatically by the agent.
Start an interactive session where you can give instructions to the browser agent.
# Basic usage (uses LLM_PROVIDER and LLM_MODEL from .env)
uv run pb-agent chat
# Override LLM provider and model
uv run pb-agent chat --llm-provider anthropic --llm-model claude-3-opus-20240229
# Record screenshots after each action
uv run pb-agent chat --record --screenshot-dir my_chat_screenshotsInside the chat, type your instructions (e.g., navigate to google.com, search for playwright documentation, click the first link). Type exit or bye to end the session.
Execute a sequence of instructions from a file.
-
Create an instruction file (e.g.,
tasks.txt):navigate to https://google.com type "Playwright Python" into the search bar and submit click the link containing "Playwright for Python | Playwright Python" take a screenshot -
Run the batch command:
# Basic usage uv run pb-agent batch tasks.txt # With recording and custom screenshot directory uv run pb-agent batch tasks.txt --record --screenshot-dir my_batch_screenshots # Override LLM for the batch run uv run pb-agent batch tasks.txt --llm-provider openai --llm-model gpt-3.5-turbo
--llm-provider/-p: Specify the LLM provider (overrides.env).--llm-model/-m: Specify the LLM model (overrides.env).--record: Enable saving screenshots to a timestamped subfolder within the directory specified by--screenshot-dir. The actual files will be namedstep_<N>.png.--screenshot-dir: Specify the base directory for saving screenshots (defaults toartifacts). Used in conjunction with--record.--playwright-mcp-config-path: Path to a custom Playwright MCP JSON configuration file.
- Testing: Install dev dependencies (
uv sync --all-extras) and run tests withuv run pytest. - Dependencies: Managed using
uv. Useuv add <package>oruv remove <package>.