This Python library provides functionality to extract and highlight interactive elements in a web page using DOM-based analysis. It implements the following features:
- Extract all interactive and clickable elements from a webpage
- Highlight these elements with colored bounding boxes in the browser
- Take screenshots with the highlighted elements
- Interact with elements by clicking on them
- Provide detailed information about each interactive element
- Execute complex browsing tasks with AI assistance
- Break down tasks into logical steps and execute them automatically
- Clone the repository:
git clone https://github.com/yourusername/browser-use.git
cd browser-use- Install the required dependencies:
pip install -r requirements.txt- Install Playwright browsers:
playwright install- Set up API keys (for AI-powered features):
- Copy
.env.exampleto.envand fill in your API keys - Or set environment variables manually (see AI Features section below)
- Copy
To see the basic DOM extraction in action, run the demo script:
python demo.pyThis will:
- Open a browser window
- Navigate to example.com
- Extract and highlight all interactive elements
- Save a screenshot with the highlights
- Click on the first interactive element
- Save a screenshot after clicking
Here's a simple example of how to use the library in your own code:
import asyncio
from playwright.async_api import async_playwright
from browser_use.dom.service import DomService
async def example():
async with async_playwright() as p:
# Launch browser
browser = await p.chromium.launch(headless=False)
page = await browser.new_page()
# Navigate to a website
await page.goto("https://example.com")
await page.wait_for_load_state("networkidle")
# Initialize DOM service
dom_service = DomService(page)
# Extract and highlight clickable elements
dom_state = await dom_service.get_clickable_elements()
# Print information about each element
for idx, element in dom_state.selector_map.items():
print(f"Element {idx}: {element}")
# Take a screenshot with highlights
await dom_service.take_screenshot_with_highlights("screenshot.png")
# Click on an element by index
if 0 in dom_state.selector_map:
await dom_service.click_element(0)
# Close the browser
await browser.close()
# Run the example
asyncio.run(example())