@@ -43,6 +43,12 @@ def __init__(self, stagehand_instance):
43
43
# Use object.__setattr__ to avoid infinite recursion
44
44
object .__setattr__ (self , "_stagehand" , stagehand_instance )
45
45
46
+ async def _ensure_page_stability (self ):
47
+ """Wait for any pending page switches to complete"""
48
+ if hasattr (self ._stagehand , '_page_switch_lock' ):
49
+ async with self ._stagehand ._page_switch_lock :
50
+ pass # Just wait for any ongoing switches
51
+
46
52
def __getattr__ (self , name ):
47
53
"""Delegate all attribute access to the current active page."""
48
54
stagehand = object .__getattribute__ (self , "_stagehand" )
@@ -55,13 +61,13 @@ def __getattr__(self, name):
55
61
else :
56
62
raise RuntimeError ("No active page available" )
57
63
58
- # Get the attribute from the active page
64
+ # For async operations, make them wait for stability
59
65
attr = getattr (active_page , name )
60
-
61
- # If it's a method, bind it to the active page
62
- if callable ( attr ):
63
- return attr
64
-
66
+ if callable ( attr ) and asyncio . iscoroutinefunction ( attr ):
67
+ async def wrapped ( * args , ** kwargs ):
68
+ await self . _ensure_page_stability ()
69
+ return await attr ( * args , ** kwargs )
70
+ return wrapped
65
71
return attr
66
72
67
73
def __setattr__ (self , name , value ):
0 commit comments