mirror of
https://github.com/browser-use/browser-use
synced 2026-05-06 17:52:15 +02:00
112 lines
2.4 KiB
Plaintext
112 lines
2.4 KiB
Plaintext
---
|
|
title: "Examples"
|
|
description: "Comprehensive examples for Browser Actor automation tasks including forms, JavaScript, mouse operations, and AI features"
|
|
icon: "code-simple"
|
|
mode: "wide"
|
|
---
|
|
|
|
## Page Management
|
|
|
|
```python
|
|
from browser_use import Browser
|
|
|
|
browser = Browser()
|
|
await browser.start()
|
|
|
|
# Create pages
|
|
page = await browser.new_page() # Blank tab
|
|
page = await browser.new_page("https://example.com") # With URL
|
|
|
|
# Get all pages
|
|
pages = await browser.get_pages()
|
|
current = await browser.get_current_page()
|
|
|
|
# Close page
|
|
await browser.close_page(page)
|
|
await browser.stop()
|
|
```
|
|
|
|
## Element Finding & Interactions
|
|
|
|
```python
|
|
page = await browser.new_page('https://github.com')
|
|
|
|
# CSS selectors (immediate return)
|
|
elements = await page.get_elements_by_css_selector("input[type='text']")
|
|
buttons = await page.get_elements_by_css_selector("button.submit")
|
|
|
|
# Element actions
|
|
await elements[0].click()
|
|
await elements[0].fill("Hello World")
|
|
await elements[0].hover()
|
|
|
|
# Page actions
|
|
await page.press("Enter")
|
|
screenshot = await page.screenshot()
|
|
```
|
|
|
|
## LLM-Powered Features
|
|
|
|
```python
|
|
from browser_use.llm.openai import ChatOpenAI
|
|
from pydantic import BaseModel
|
|
|
|
llm = ChatOpenAI(api_key="your-api-key")
|
|
|
|
# Find elements using natural language
|
|
button = await page.get_element_by_prompt("login button", llm=llm)
|
|
await button.click()
|
|
|
|
# Extract structured data
|
|
class ProductInfo(BaseModel):
|
|
name: str
|
|
price: float
|
|
|
|
product = await page.extract_content(
|
|
"Extract product name and price",
|
|
ProductInfo,
|
|
llm=llm
|
|
)
|
|
```
|
|
|
|
## JavaScript Execution
|
|
|
|
```python
|
|
# Simple JavaScript evaluation
|
|
title = await page.evaluate('() => document.title')
|
|
|
|
# JavaScript with arguments
|
|
result = await page.evaluate('(x, y) => x + y', 10, 20)
|
|
|
|
# Complex operations
|
|
stats = await page.evaluate('''() => ({
|
|
url: location.href,
|
|
links: document.querySelectorAll('a').length
|
|
})''')
|
|
```
|
|
|
|
## Mouse Operations
|
|
|
|
```python
|
|
mouse = await page.mouse
|
|
|
|
# Click at coordinates
|
|
await mouse.click(x=100, y=200)
|
|
|
|
# Drag and drop
|
|
await mouse.down()
|
|
await mouse.move(x=500, y=600)
|
|
await mouse.up()
|
|
|
|
# Scroll
|
|
await mouse.scroll(x=0, y=100, delta_y=-500)
|
|
```
|
|
|
|
## Best Practices
|
|
|
|
- Use `asyncio.sleep()` after actions that trigger navigation
|
|
- Check URL/title changes to verify state transitions
|
|
- Always check if elements exist before interaction
|
|
- Implement retry logic for flaky elements
|
|
- Call `browser.stop()` to clean up resources
|