Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

Add direct action call API to Tools via __getattr__

Magnus Müllerc9137fe0296e
diff --git a/browser_use/tools/service.py b/browser_use/tools/service.py
index 976ce75d4..eadaf5fdd 100644
--- a/browser_use/tools/service.py
+++ b/browser_use/tools/service.py
@@ -1274,6 +1274,37 @@ Validated Code (after quote fixing):
raise ValueError(f'Invalid action result type: {type(result)} of {result}')
return ActionResult()
+ def __getattr__(self, name: str):
+ """
+ Enable direct action calls like tools.navigate(url=..., browser_session=...).
+ This provides a simpler API for tests and direct usage while maintaining backward compatibility.
+ """
+ # Check if this is a registered action
+ if name in self.registry.registry.actions:
+ # Create a wrapper that delegates to registry.execute_action
+ async def action_wrapper(**kwargs):
+ # Separate action params from special params (injected dependencies)
+ special_param_names = {'browser_session', 'page_extraction_llm', 'file_system', 'available_file_paths', 'sensitive_data'}
+
+ # Extract action params (params for the action itself)
+ action_params = {k: v for k, v in kwargs.items() if k not in special_param_names}
+
+ # Extract special params (injected dependencies)
+ special_kwargs = {k: v for k, v in kwargs.items() if k in special_param_names}
+
+ # Delegate to the existing registry.execute_action method
+ # This ensures all the logic (sensitive_data, page_url extraction, error handling, etc.) is centralized
+ return await self.registry.execute_action(
+ action_name=name,
+ params=action_params,
+ **special_kwargs
+ )
+
+ return action_wrapper
+
+ # If not an action, raise AttributeError for normal Python behavior
+ raise AttributeError(f"'{type(self).__name__}' object has no attribute '{name}'")
+
# Alias for backwards compatibility
Controller = Tools