|
| 1 | +""" |
| 2 | +Example: use a Pydoll Tab with the Stagehand Python SDK. |
| 3 | +
|
| 4 | +What this demonstrates: |
| 5 | +- Start a Stagehand session (remote Stagehand API / Browserbase browser) |
| 6 | +- Attach Pydoll to the same browser via CDP (`cdp_url`) |
| 7 | +- Use Pydoll to navigate |
| 8 | +- Fetch the current page's `frame_id` via CDP `Page.getFrameTree` |
| 9 | +- Fetch the current page's `frame_id` via CDP ePage.getFrameTree` |
| 10 | +- Pass `frame_id` into `session.observe/act/extract` |
| 11 | +
|
| 12 | +Environment variables required: |
| 13 | +- MODEL_API_KEY |
| 14 | +- BROWSERBASE_API_KEY |
| 15 | +- BROWSERBASE_PROJECT_ID |
| 16 | +
|
| 17 | +Optional: |
| 18 | +- STAGEHAND_BASE_URL (defaults to https://api.stagehand.browserbase.com) |
| 19 | +
|
| 20 | +Notes: |
| 21 | +- This example requires Python 3.10+ because `pydoll-python` requires Python 3.10+. |
| 22 | +- If this repo is pinned to an older Python via `.python-version`, run with: |
| 23 | + - `uv run --python 3.12 python examples/pydoll_tab_example.py` |
| 24 | +""" |
| 25 | + |
| 26 | +from __future__ import annotations |
| 27 | + |
| 28 | +import os |
| 29 | +import sys |
| 30 | +import asyncio |
| 31 | +from typing import Any |
| 32 | + |
| 33 | +from stagehand import AsyncStagehand |
| 34 | + |
| 35 | + |
| 36 | +def _normalize_ws_address_for_pydoll(cdp_url: str) -> str: |
| 37 | + # Pydoll currently validates the address strictly as `ws://...` (not `wss://...`). |
| 38 | + if cdp_url.startswith("ws://"): |
| 39 | + return cdp_url |
| 40 | + if cdp_url.startswith("wss://"): |
| 41 | + return "ws://" + cdp_url.removeprefix("wss://") |
| 42 | + if cdp_url.startswith("http://"): |
| 43 | + return "ws://" + cdp_url.removeprefix("http://") |
| 44 | + if cdp_url.startswith("https://"): |
| 45 | + return "ws://" + cdp_url.removeprefix("https://") |
| 46 | + raise RuntimeError(f"Unsupported CDP URL scheme for Pydoll: {cdp_url!r}") |
| 47 | + |
| 48 | + |
| 49 | +async def _pydoll_attach_to_tab_session(*, chrome: Any, tab: Any) -> tuple[Any, str]: |
| 50 | + """ |
| 51 | + Attach to the tab target via CDP Target.attachToTarget (flatten mode) and return (handler, session_id). |
| 52 | +
|
| 53 | + For some CDP proxies (including Browserbase connect URLs), the `/devtools/page/<id>` endpoints may not |
| 54 | + behave like local Chrome's endpoints. Attaching and sending commands with `sessionId` is the most |
| 55 | + compatible approach. |
| 56 | + """ |
| 57 | + handler = getattr(chrome, "_connection_handler", None) |
| 58 | + if handler is None: |
| 59 | + raise RuntimeError("Could not find Pydoll browser connection handler on `chrome`.") |
| 60 | + |
| 61 | + target_id = getattr(tab, "_target_id", None) or getattr(tab, "target_id", None) |
| 62 | + if not target_id: |
| 63 | + raise RuntimeError("Could not find a target id on the tab (expected `tab._target_id`).") |
| 64 | + |
| 65 | + attached = await handler.execute_command( |
| 66 | + { |
| 67 | + "method": "Target.attachToTarget", |
| 68 | + "params": {"targetId": target_id, "flatten": True}, |
| 69 | + }, |
| 70 | + timeout=60, |
| 71 | + ) |
| 72 | + try: |
| 73 | + return handler, attached["result"]["sessionId"] |
| 74 | + except Exception as e: # noqa: BLE001 |
| 75 | + raise RuntimeError("Failed to attach to target and get sessionId") from e |
| 76 | + |
| 77 | + |
| 78 | +async def _pydoll_execute_on_session(*, handler: Any, session_id: str, command: dict[str, Any]) -> dict[str, Any]: |
| 79 | + cmd = dict(command) |
| 80 | + cmd["sessionId"] = session_id |
| 81 | + return await handler.execute_command(cmd, timeout=60) |
| 82 | + |
| 83 | + |
| 84 | +async def _pydoll_session_to_frame_id(*, handler: Any, session_id: str) -> str: |
| 85 | + response = await _pydoll_execute_on_session( |
| 86 | + handler=handler, |
| 87 | + session_id=session_id, |
| 88 | + command={"method": "Page.getFrameTree", "params": {}}, |
| 89 | + ) |
| 90 | + try: |
| 91 | + return response["result"]["frameTree"]["frame"]["id"] |
| 92 | + except Exception as e: # noqa: BLE001 |
| 93 | + raise RuntimeError("Failed to extract frame id from CDP Page.getFrameTree response") from e |
| 94 | + |
| 95 | + |
| 96 | +async def main() -> None: |
| 97 | + model_api_key = os.environ.get("MODEL_API_KEY") |
| 98 | + if not model_api_key: |
| 99 | + sys.exit("Set the MODEL_API_KEY environment variable to run this example.") |
| 100 | + |
| 101 | + bb_api_key = os.environ.get("BROWSERBASE_API_KEY") |
| 102 | + bb_project_id = os.environ.get("BROWSERBASE_PROJECT_ID") |
| 103 | + if not bb_api_key or not bb_project_id: |
| 104 | + sys.exit("Set BROWSERBASE_API_KEY and BROWSERBASE_PROJECT_ID to run this example.") |
| 105 | + |
| 106 | + try: |
| 107 | + from pydoll.browser.chromium import Chrome # type: ignore[import-not-found] |
| 108 | + except Exception: |
| 109 | + sys.exit( |
| 110 | + "Pydoll is not installed. Install it with:\n" |
| 111 | + " uv pip install pydoll-python\n" |
| 112 | + "or:\n" |
| 113 | + " pip install pydoll-python\n" |
| 114 | + ) |
| 115 | + |
| 116 | + async with AsyncStagehand( |
| 117 | + server="remote", |
| 118 | + browserbase_api_key=bb_api_key, |
| 119 | + browserbase_project_id=bb_project_id, |
| 120 | + model_api_key=model_api_key, |
| 121 | + ) as client: |
| 122 | + print("⏳ Starting Stagehand session...") |
| 123 | + session = await client.sessions.create( |
| 124 | + model_name="openai/gpt-5-nano", |
| 125 | + browser={"type": "browserbase"}, |
| 126 | + ) |
| 127 | + |
| 128 | + cdp_url = session.data.cdp_url |
| 129 | + if not cdp_url: |
| 130 | + sys.exit("No cdp_url returned from the API for this session; cannot attach Pydoll.") |
| 131 | + |
| 132 | + print(f"✅ Session started: {session.id}") |
| 133 | + print("🔌 Connecting Pydoll to the same browser over CDP...") |
| 134 | + |
| 135 | + chrome = Chrome() |
| 136 | + try: |
| 137 | + ws_address = _normalize_ws_address_for_pydoll(cdp_url) |
| 138 | + if ws_address != cdp_url: |
| 139 | + print(f"ℹ️ Normalized cdp_url for Pydoll: {ws_address}") |
| 140 | + tab = await chrome.connect(ws_address) |
| 141 | + |
| 142 | + handler, session_id = await _pydoll_attach_to_tab_session(chrome=chrome, tab=tab) |
| 143 | + |
| 144 | + await _pydoll_execute_on_session( |
| 145 | + handler=handler, |
| 146 | + session_id=session_id, |
| 147 | + command={"method": "Page.enable", "params": {}}, |
| 148 | + ) |
| 149 | + await _pydoll_execute_on_session( |
| 150 | + handler=handler, |
| 151 | + session_id=session_id, |
| 152 | + command={"method": "Runtime.enable", "params": {}}, |
| 153 | + ) |
| 154 | + |
| 155 | + # Navigate a bit using CDP (via the attached session). |
| 156 | + await _pydoll_execute_on_session( |
| 157 | + handler=handler, |
| 158 | + session_id=session_id, |
| 159 | + command={"method": "Page.navigate", "params": {"url": "https://example.com"}}, |
| 160 | + ) |
| 161 | + await asyncio.sleep(2) |
| 162 | + |
| 163 | + await _pydoll_execute_on_session( |
| 164 | + handler=handler, |
| 165 | + session_id=session_id, |
| 166 | + command={ |
| 167 | + "method": "Page.navigate", |
| 168 | + "params": {"url": "https://www.iana.org/domains/reserved"}, |
| 169 | + }, |
| 170 | + ) |
| 171 | + await asyncio.sleep(2) |
| 172 | + |
| 173 | + frame_id = await _pydoll_session_to_frame_id(handler=handler, session_id=session_id) |
| 174 | + print(f"🧩 frame_id: {frame_id}") |
| 175 | + |
| 176 | + print("👀 Stagehand.observe(frame_id=...) ...") |
| 177 | + actions = await session.observe( |
| 178 | + instruction="Find the most relevant click target on this page", |
| 179 | + frame_id=frame_id, |
| 180 | + ) |
| 181 | + print(f"Observed {len(actions.data.result)} actions") |
| 182 | + |
| 183 | + print("🧠 Stagehand.extract(frame_id=...) ...") |
| 184 | + extracted = await session.extract( |
| 185 | + instruction="Extract the page title and the primary heading (h1) text", |
| 186 | + schema={ |
| 187 | + "type": "object", |
| 188 | + "properties": { |
| 189 | + "title": {"type": "string"}, |
| 190 | + "h1": {"type": "string"}, |
| 191 | + }, |
| 192 | + "required": ["title", "h1"], |
| 193 | + "additionalProperties": False, |
| 194 | + }, |
| 195 | + frame_id=frame_id, |
| 196 | + ) |
| 197 | + print("Extracted:", extracted.data.result) |
| 198 | + |
| 199 | + finally: |
| 200 | + close = getattr(chrome, "close", None) |
| 201 | + if callable(close): |
| 202 | + await close() |
| 203 | + await session.end() |
| 204 | + |
| 205 | + |
| 206 | +if __name__ == "__main__": |
| 207 | + asyncio.run(main()) |
0 commit comments