ai-agent-book 精选快照(<2MB 代码与文档,来自 github.com/bojieli/ai-agent-book)
Build latest book artifacts / build (push) Canceled after 0s
dependency resolution / resolve (3.11) (push) Canceled after 0s
dependency resolution / resolve (3.13) (push) Canceled after 0s
deploy-pages / build (push) Canceled after 0s
deploy-pages / deploy (push) Canceled after 0s
i18n consistency check / check (push) Canceled after 0s
provider adoption tests / test (chapter2/context-compression) (push) Canceled after 0s
provider adoption tests / test (chapter2/prompt-injection) (push) Canceled after 0s
provider adoption tests / test (chapter2/system-hint) (push) Canceled after 0s
provider adoption tests / test (chapter3/log-sanitization) (push) Canceled after 0s
web-search-agent tests / test (push) Canceled after 0s
web-search-agent tests / agentbook (push) Canceled after 0s
Build latest book artifacts / build (push) Canceled after 0s
dependency resolution / resolve (3.11) (push) Canceled after 0s
dependency resolution / resolve (3.13) (push) Canceled after 0s
deploy-pages / build (push) Canceled after 0s
deploy-pages / deploy (push) Canceled after 0s
i18n consistency check / check (push) Canceled after 0s
provider adoption tests / test (chapter2/context-compression) (push) Canceled after 0s
provider adoption tests / test (chapter2/prompt-injection) (push) Canceled after 0s
provider adoption tests / test (chapter2/system-hint) (push) Canceled after 0s
provider adoption tests / test (chapter3/log-sanitization) (push) Canceled after 0s
web-search-agent tests / test (push) Canceled after 0s
web-search-agent tests / agentbook (push) Canceled after 0s
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
---
|
||||
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
|
||||
Reference in New Issue
Block a user