Skip to content

Commit cc41c3e

Browse files
derekmeeganclaude
andcommitted
Catch TargetClosedError instead of matching error string
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent c918f29 commit cc41c3e

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

stagehand/page.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import time
33
from typing import Optional, Union
44

5+
from playwright._impl._errors import TargetClosedError
56
from playwright.async_api import CDPSession, Page
67
from pydantic import BaseModel
78

@@ -75,14 +76,11 @@ async def ensure_injection(self):
7576
await self._page.evaluate(_INJECTION_SCRIPT)
7677
# Ensure that the script is injected on future navigations
7778
await self._page.add_init_script(_INJECTION_SCRIPT)
78-
except Exception as e:
79-
if "Execution context was destroyed" in str(e):
80-
self._stagehand.logger.warning(
81-
f"ensure_injection failed (page may be navigating): {e}",
82-
category="page",
83-
)
84-
else:
85-
raise
79+
except TargetClosedError as e:
80+
self._stagehand.logger.warning(
81+
f"ensure_injection failed (page may be navigating): {e}",
82+
category="page",
83+
)
8684

8785
async def goto(
8886
self,

0 commit comments

Comments
 (0)