|
27 | 27 | <img alt="MIT License" src="https://stagehand.dev/api/assets/license?mode=light" /> |
28 | 28 | </picture> |
29 | 29 | </a> |
30 | | - <a href="https://join.slack.com/t/stagehand-dev/shared_invite/zt-2tdncfgkk-fF8y5U0uJzR2y2_M9c9OJA"> |
| 30 | + <a href="https://stagehand.dev/slack"> |
31 | 31 | <picture> |
32 | 32 | <source media="(prefers-color-scheme: dark)" srcset="https://stagehand.dev/api/assets/slack?mode=dark" /> |
33 | 33 | <img alt="Slack Community" src="https://stagehand.dev/api/assets/slack?mode=light" /> |
@@ -60,6 +60,91 @@ await stagehand.page.observe("find the search bar") |
60 | 60 | await stagehand.agent.execute("book a reservation for 2 people for a trip to the Maldives") |
61 | 61 | ``` |
62 | 62 |
|
| 63 | +## Quickstart |
| 64 | + |
| 65 | +```python |
| 66 | +import asyncio |
| 67 | +import os |
| 68 | +from dotenv import load_dotenv |
| 69 | +from pydantic import BaseModel, Field, HttpUrl |
| 70 | + |
| 71 | +from stagehand import StagehandConfig, Stagehand |
| 72 | +from stagehand.types import ExtractOptions |
| 73 | + |
| 74 | +# Load environment variables |
| 75 | +load_dotenv() |
| 76 | + |
| 77 | +# Define Pydantic models for structured data extraction |
| 78 | +class Company(BaseModel): |
| 79 | + name: str = Field(..., description="The name of the company") |
| 80 | + url: HttpUrl = Field(..., description="The URL of the company website or relevant page") |
| 81 | + |
| 82 | +class Companies(BaseModel): |
| 83 | + companies: list[Company] = Field(..., description="List of companies extracted from the page, maximum of 5 companies") |
| 84 | + |
| 85 | +async def main(): |
| 86 | + # Create configuration |
| 87 | + config = StagehandConfig( |
| 88 | + api_key=os.getenv("BROWSERBASE_API_KEY"), |
| 89 | + project_id=os.getenv("BROWSERBASE_PROJECT_ID"), |
| 90 | + model_name="gpt-4o", |
| 91 | + model_client_options={"apiKey": os.getenv("MODEL_API_KEY")}, |
| 92 | + verbose=1, |
| 93 | + ) |
| 94 | + |
| 95 | + # Initialize async client |
| 96 | + stagehand = Stagehand( |
| 97 | + env=os.getenv("STAGEHAND_ENV"), |
| 98 | + config=config, |
| 99 | + api_url=os.getenv("STAGEHAND_SERVER_URL"), |
| 100 | + ) |
| 101 | + |
| 102 | + try: |
| 103 | + # Initialize the client |
| 104 | + await stagehand.init() |
| 105 | + print("✓ Successfully initialized Stagehand async client") |
| 106 | + |
| 107 | + # Navigate to AIgrant |
| 108 | + await stagehand.page.goto("https://www.aigrant.com") |
| 109 | + print("✓ Navigated to AIgrant") |
| 110 | + |
| 111 | + # Click the "Get Started" button using AI |
| 112 | + await stagehand.page.act("click the button with text 'Get Started'") |
| 113 | + print("✓ Clicked 'Get Started' button") |
| 114 | + |
| 115 | + # Observe elements on the page |
| 116 | + observed = await stagehand.page.observe("the button with text 'Get Started'") |
| 117 | + print("✓ Observed 'Get Started' button") |
| 118 | + |
| 119 | + # Extract companies using structured schema |
| 120 | + extract_options = ExtractOptions( |
| 121 | + instruction="Extract the names and URLs of up to 5 companies mentioned on this page", |
| 122 | + schema_definition=Companies.model_json_schema() |
| 123 | + ) |
| 124 | + |
| 125 | + companies_data = await stagehand.page.extract(extract_options) |
| 126 | + print("✓ Extracted companies data") |
| 127 | + |
| 128 | + # Display results |
| 129 | + print("\nExtracted Companies:") |
| 130 | + if hasattr(companies_data, "companies"): |
| 131 | + for idx, company in enumerate(companies_data.companies, 1): |
| 132 | + print(f"{idx}. {company.name}: {company.url}") |
| 133 | + else: |
| 134 | + print("No companies were found in the extraction result") |
| 135 | + |
| 136 | + except Exception as e: |
| 137 | + print(f"Error during testing: {str(e)}") |
| 138 | + raise |
| 139 | + finally: |
| 140 | + # Close the client |
| 141 | + await stagehand.close() |
| 142 | + print("Stagehand async client closed") |
| 143 | + |
| 144 | +if __name__ == "__main__": |
| 145 | + asyncio.run(main()) |
| 146 | +``` |
| 147 | + |
63 | 148 | ## Installation |
64 | 149 |
|
65 | 150 | ### Creating a Virtual Environment (Recommended) |
@@ -129,60 +214,7 @@ export STAGEHAND_API_URL="url-of-stagehand-server" # if running remotely |
129 | 214 | export STAGEHAND_ENV="BROWSERBASE" # or "LOCAL" to run Stagehand locally |
130 | 215 | ``` |
131 | 216 |
|
132 | | -You can also make a copy of `.env.example` and add these to your `.env` file. |
133 | | - |
134 | | -## Quickstart |
135 | | - |
136 | | -```python |
137 | | -import os |
138 | | -import asyncio |
139 | | -from stagehand import Stagehand, StagehandConfig |
140 | | -from dotenv import load_dotenv |
141 | | - |
142 | | -load_dotenv() |
143 | | - |
144 | | -async def main(): |
145 | | - # Configure Stagehand |
146 | | - config = StagehandConfig( |
147 | | - env="BROWSERBASE", |
148 | | - api_key=os.getenv("BROWSERBASE_API_KEY"), |
149 | | - project_id=os.getenv("BROWSERBASE_PROJECT_ID"), |
150 | | - model_name="gpt-4o", |
151 | | - model_client_options={"apiKey": os.getenv("MODEL_API_KEY")} |
152 | | - ) |
153 | | - |
154 | | - # Initialize Stagehand |
155 | | - stagehand = Stagehand(config=config, api_url=os.getenv("STAGEHAND_API_URL")) |
156 | | - await stagehand.init() |
157 | | - print(f"Session created: {stagehand.session_id}") |
158 | | - |
159 | | - # Get page reference |
160 | | - page = stagehand.page |
161 | | - |
162 | | - # Navigate to a page |
163 | | - await page.goto("https://google.com/") |
164 | | - |
165 | | - # Use Stagehand AI primitives |
166 | | - await page.act("search for openai") |
167 | | - |
168 | | - # Combine with Playwright |
169 | | - await page.keyboard.press("Enter") |
170 | | - |
171 | | - # Observe elements on the page |
172 | | - observed = await page.observe("find the news button") |
173 | | - if observed: |
174 | | - await page.act(observed[0]) # Act on the first observed element |
175 | | - |
176 | | - # Extract data from the page |
177 | | - data = await page.extract("extract the first result from the search") |
178 | | - print(f"Extracted data: {data}") |
179 | | - |
180 | | - # Close the session |
181 | | - await stagehand.close() |
182 | | - |
183 | | -if __name__ == "__main__": |
184 | | - asyncio.run(main()) |
185 | | -``` |
| 217 | +You can also make a copy of `.env.example` and add these to your `.env` file. |
186 | 218 |
|
187 | 219 | ## Agent Example |
188 | 220 |
|
|
0 commit comments