Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
add retry for actions that fail due to page navigation mid-action
Nick Sweeting60f2abd2069b
diff --git a/browser_use/controller/registry/service.py b/browser_use/controller/registry/service.pyindex 594f6f93c..8e46467c8 100644--- a/browser_use/controller/registry/service.py+++ b/browser_use/controller/registry/service.py@@ -353,7 +353,20 @@ class Registry(Generic[Context]):# All functions are now normalized to accept kwargs only# Call with params and unpacked special context- return await action.function(params=validated_params, **special_context)+ try:+ return await action.function(params=validated_params, **special_context)+ except Exception as e:+ # Retry once if it's a page error+ if 'page' in str(e) and browser_session and 'page' in special_context:+ logger.warning(f'Page error in {action_name}, retrying with fresh page')+ special_context['page'] = await browser_session.get_current_page()+ try:+ return await action.function(params=validated_params, **special_context)+ except Exception:+ raise RuntimeError(+ f'Action {action_name} failed: The page may have been closed or navigated away during execution'+ ) from e+ raiseexcept ValueError as e:# Preserve ValueError messages from validationdiff --git a/tests/ci/test_registry.py b/tests/ci/test_registry.pyindex a4bba5794..591250009 100644--- a/tests/ci/test_registry.py+++ b/tests/ci/test_registry.py@@ -993,6 +993,23 @@ class TestParameterOrdering:assert action.name == 'extract_content'assert action.description == 'Extract content from page'+ async def test_page_error_retry(self, registry, browser_session):+ """Test that page errors trigger retry with fresh page"""+ call_count = 0++ @registry.action('Flaky page action', param_model=SimpleParams)+ async def flaky_action(params: SimpleParams, page: Page):+ nonlocal call_count+ call_count += 1+ if call_count == 1:+ raise RuntimeError('page closed')+ return ActionResult(extracted_content=f'Success on attempt {call_count}')++ # Should retry once and succeed+ result = await registry.execute_action('flaky_action', {'value': 'test'}, browser_session=browser_session)+ assert 'Success on attempt 2' in result.extracted_content+ assert call_count == 2+class TestParamsModelArgsAndKwargs:async def test_browser_session_double_kwarg(self):