@@ -43,6 +43,12 @@ def __init__(self, stagehand_instance):
4343 # Use object.__setattr__ to avoid infinite recursion
4444 object .__setattr__ (self , "_stagehand" , stagehand_instance )
4545
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+
4652 def __getattr__ (self , name ):
4753 """Delegate all attribute access to the current active page."""
4854 stagehand = object .__getattribute__ (self , "_stagehand" )
@@ -55,13 +61,13 @@ def __getattr__(self, name):
5561 else :
5662 raise RuntimeError ("No active page available" )
5763
58- # Get the attribute from the active page
64+ # For async operations, make them wait for stability
5965 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
6571 return attr
6672
6773 def __setattr__ (self , name , value ):
0 commit comments