from crewai import Agent, Task, Crewfrom crewai_tools import StagehandToolfrom stagehand.schemas import AvailableModel# Initialize the tool with your API keys using a context managerwith StagehandTool( api_key="your-browserbase-api-key", project_id="your-browserbase-project-id", model_api_key="your-llm-api-key", # OpenAI 또는 Anthropic API 키 model_name=AvailableModel.CLAUDE_3_7_SONNET_LATEST, # 선택 사항: 사용할 모델 지정) as stagehand_tool: # Create an agent with the tool researcher = Agent( role="Web Researcher", goal="Find and summarize information from websites", backstory="I'm an expert at finding information online.", verbose=True, tools=[stagehand_tool], ) # Create a task that uses the tool research_task = Task( description="Go to https://www.example.com and tell me what you see on the homepage.", agent=researcher, ) # Run the crew crew = Crew( agents=[researcher], tasks=[research_task], verbose=True, ) result = crew.kickoff() print(result)
from crewai import Agent, Task, Crewfrom crewai_tools import StagehandToolfrom stagehand.schemas import AvailableModel# Initialize the tool with your API keysstagehand_tool = StagehandTool( api_key="your-browserbase-api-key", project_id="your-browserbase-project-id", model_api_key="your-llm-api-key", model_name=AvailableModel.CLAUDE_3_7_SONNET_LATEST,)try: # Create an agent with the tool researcher = Agent( role="Web Researcher", goal="Find and summarize information from websites", backstory="I'm an expert at finding information online.", verbose=True, tools=[stagehand_tool], ) # Create a task that uses the tool research_task = Task( description="Go to https://www.example.com and tell me what you see on the homepage.", agent=researcher, ) # Run the crew crew = Crew( agents=[researcher], tasks=[research_task], verbose=True, ) result = crew.kickoff() print(result)finally: # Explicitly clean up resources stagehand_tool.close()
act 명령어 유형(기본값)은 버튼 클릭, 양식 작성, 내비게이션과 같은 웹페이지 상호작용을 활성화합니다.
# Perform an action (default behavior)result = stagehand_tool.run( instruction="Click the login button", url="https://example.com", command_type="act" # Default, so can be omitted)# Fill out a formresult = stagehand_tool.run( instruction="Fill the contact form with name 'John Doe', email 'john@example.com', and message 'Hello world'", url="https://example.com/contact")
# 모든 상품 정보 추출result = stagehand_tool.run( instruction="Extract all product names, prices, and descriptions", url="https://example.com/products", command_type="extract")# 선택자를 사용하여 특정 정보 추출result = stagehand_tool.run( instruction="Extract the main article title and content", url="https://example.com/blog/article", command_type="extract", selector=".article-container" # 선택적 CSS 선택자)
# 인터랙티브 요소 찾기result = stagehand_tool.run( instruction="Find all interactive elements in the navigation menu", url="https://example.com", command_type="observe")# 폼 필드 식별result = stagehand_tool.run( instruction="Identify all the input fields in the registration form", url="https://example.com/register", command_type="observe", selector="#registration-form")
stagehand_tool = StagehandTool( api_key="your-browserbase-api-key", project_id="your-browserbase-project-id", model_api_key="your-llm-api-key", model_name=AvailableModel.CLAUDE_3_7_SONNET_LATEST, dom_settle_timeout_ms=5000, # DOM이 안정될 때까지 더 오래 대기 headless=True, # 브라우저를 헤드리스 모드로 실행 self_heal=True, # 오류에서 복구를 시도 wait_for_captcha_solves=True, # CAPTCHA 해결을 기다림 verbose=1, # 로깅 상세 수준 제어 (0-3))