-
Notifications
You must be signed in to change notification settings - Fork 98
Made frameId optional #266
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| from __future__ import annotations | ||
|
|
||
| """ | ||
| Example showing how to bring your own browser driver while still using Stagehand. | ||
|
|
||
| This script runs Playwright locally to drive the browser and uses Stagehand to | ||
| plan the interactions (observe → extract) without having Stagehand own the page. | ||
|
|
||
| Required environment variables: | ||
| - BROWSERBASE_API_KEY | ||
| - BROWSERBASE_PROJECT_ID | ||
| - MODEL_API_KEY | ||
|
|
||
| Usage: | ||
|
|
||
| ``` | ||
| pip install playwright stagehand-alpha | ||
| # (if Playwright is new) playwright install chromium | ||
| uv run python examples/byob_example.py | ||
| ``` | ||
| """ | ||
|
|
||
| import os | ||
| import asyncio | ||
|
|
||
| from playwright.async_api import async_playwright | ||
|
|
||
| from stagehand import AsyncStagehand | ||
|
|
||
|
|
||
| async def main() -> None: | ||
| async with AsyncStagehand( | ||
| browserbase_api_key=os.environ.get("BROWSERBASE_API_KEY"), | ||
| browserbase_project_id=os.environ.get("BROWSERBASE_PROJECT_ID"), | ||
| model_api_key=os.environ.get("MODEL_API_KEY"), | ||
| ) as client, async_playwright() as playwright: | ||
| browser = await playwright.chromium.launch(headless=True) | ||
| page = await browser.new_page() | ||
| session = await client.sessions.create(model_name="openai/gpt-5-nano") | ||
|
|
||
| try: | ||
| target_url = "https://news.ycombinator.com" | ||
| await session.navigate(url=target_url) | ||
| await page.goto(target_url, wait_until="networkidle") | ||
|
|
||
| print("🎯 Stagehand already navigated to Hacker News; Playwright now drives that page.") | ||
|
|
||
| # Click the first story's comments link with Playwright. | ||
| comments_selector = "tr.athing:first-of-type + tr .subline > a[href^='item?id=']:nth-last-of-type(1)" | ||
| await page.click(comments_selector, timeout=15_000) | ||
| await page.wait_for_load_state("networkidle") | ||
|
|
||
| print("✅ Playwright clicked the first story link.") | ||
|
|
||
| print("🔄 Syncing Stagehand to Playwright's current URL:", page.url) | ||
| await session.navigate(url=page.url) | ||
|
|
||
| extract_response = await session.extract( | ||
| instruction="extract the text of the top comment on this page", | ||
| schema={ | ||
| "type": "object", | ||
| "properties": {"comment": {"type": "string"}}, | ||
| "required": ["comment"], | ||
| }, | ||
| ) | ||
|
|
||
| print("🧮 Stagehand extraction result:", extract_response.data.result) | ||
| finally: | ||
| await session.end() | ||
| await browser.close() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main()) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P1: This example doesn't achieve the stated BYOB (Bring Your Own Browser) goal. The
AsyncStagehandclient uses the defaultserver="remote"mode, creating a browser session on Browserbase that is completely independent from the local Playwright browser. These two browsers never share the same page - they're just navigating to the same URLs independently.To actually demonstrate BYOB, consider using
server="local"mode, or clarify the example's purpose to show how to coordinate between two separate browser sessions.Prompt for AI agents