-
Notifications
You must be signed in to change notification settings - Fork 102
Wrap ensure_injection in try/catch to handle page navigation errors #313
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
base: v0
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
| import time | ||
| from typing import Optional, Union | ||
|
|
||
| from playwright._impl._errors import TargetClosedError | ||
| from playwright.async_api import CDPSession, Page | ||
| from pydantic import BaseModel | ||
|
|
||
|
|
@@ -53,28 +54,33 @@ def update_root_frame_id(self, new_id: str): | |
| self._frame_id = new_id | ||
| self._stagehand.logger.debug(f"Updated frame ID to {new_id}", category="page") | ||
|
|
||
| # TODO try catch here | ||
| async def ensure_injection(self): | ||
| """Ensure custom injection scripts are present on the page using domScripts.js.""" | ||
| exists_before = await self._page.evaluate( | ||
| "typeof window.getScrollableElementXpaths === 'function'" | ||
| ) | ||
| if not exists_before: | ||
| global _INJECTION_SCRIPT | ||
| if _INJECTION_SCRIPT is None: | ||
| import os | ||
| try: | ||
| exists_before = await self._page.evaluate( | ||
| "typeof window.getScrollableElementXpaths === 'function'" | ||
| ) | ||
| if not exists_before: | ||
| global _INJECTION_SCRIPT | ||
| if _INJECTION_SCRIPT is None: | ||
| import os | ||
|
|
||
| script_path = os.path.join(os.path.dirname(__file__), "domScripts.js") | ||
| try: | ||
| with open(script_path) as f: | ||
| _INJECTION_SCRIPT = f.read() | ||
| except Exception as e: | ||
| self._stagehand.logger.error(f"Error reading domScripts.js: {e}") | ||
| _INJECTION_SCRIPT = "/* fallback injection script */" | ||
| # Inject the script into the current page context | ||
| await self._page.evaluate(_INJECTION_SCRIPT) | ||
| # Ensure that the script is injected on future navigations | ||
| await self._page.add_init_script(_INJECTION_SCRIPT) | ||
| script_path = os.path.join(os.path.dirname(__file__), "domScripts.js") | ||
| try: | ||
| with open(script_path) as f: | ||
| _INJECTION_SCRIPT = f.read() | ||
| except Exception as e: | ||
| self._stagehand.logger.error(f"Error reading domScripts.js: {e}") | ||
| _INJECTION_SCRIPT = "/* fallback injection script */" | ||
| # Inject the script into the current page context | ||
| await self._page.evaluate(_INJECTION_SCRIPT) | ||
| # Ensure that the script is injected on future navigations | ||
| await self._page.add_init_script(_INJECTION_SCRIPT) | ||
| except TargetClosedError as e: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: Catching only Prompt for AI agents |
||
| self._stagehand.logger.warning( | ||
| f"ensure_injection failed (page may be navigating): {e}", | ||
| category="page", | ||
| ) | ||
|
|
||
| async def goto( | ||
| self, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.