Skip to content

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

fix page filter and/or domain filtering logic

Nick Sweeting82adf4520df9
diff --git a/browser_use/controller/registry/service.py b/browser_use/controller/registry/service.py
index 511a5ab0e..d46e36d70 100644
--- a/browser_use/controller/registry/service.py
+++ b/browser_use/controller/registry/service.py
@@ -195,16 +195,17 @@ class Registry(Generic[Context]):
page_filter_match = action.page_filter(page)
# Check domains if present
- domains_match = False
+ domains_match = True # Default to True if no filter
if action.domains is not None and page.url:
+ domains_match = False
# Try to match any of the domain patterns
for domain_pattern in action.domains:
if self.registry._match_domain(domain_pattern, page.url):
domains_match = True
break
- # Only include action if either filter matches
- if page_filter_match or domains_match:
+ # Include action if both filters match (or if either is not present)
+ if page_filter_match and domains_match:
available_actions[name] = action
fields = {
diff --git a/browser_use/controller/registry/views.py b/browser_use/controller/registry/views.py
index ecaed9b7e..15b17c790 100644
--- a/browser_use/controller/registry/views.py
+++ b/browser_use/controller/registry/views.py
@@ -128,16 +128,17 @@ class ActionRegistry(BaseModel):
page_filter_match = action.page_filter(page)
# Check domains if present
- domains_match = False
+ domains_match = True # Default to True if no filter
if action.domains is not None and page.url:
+ domains_match = False
# Try to match any of the domain patterns
for domain_pattern in action.domains:
if self._match_domain(domain_pattern, page.url):
domains_match = True
break
- # Include action if either filter matches
- if page_filter_match or domains_match:
+ # Include action if both filters match (or if either is not present)
+ if page_filter_match and domains_match:
filtered_actions.append(action)
return '\n'.join(action.prompt_description() for action in filtered_actions)
diff --git a/examples/use-cases/google_sheets.py b/examples/use-cases/google_sheets.py
index 5917cbe9c..73a2a2d87 100644
--- a/examples/use-cases/google_sheets.py
+++ b/examples/use-cases/google_sheets.py
@@ -29,13 +29,7 @@ controller = Controller()
def is_google_sheet(page) -> bool:
- return page.url.startswith('https://docas.google.com/spreadsheets/')
-
-
-# TODO: refactor if not is_google_sheet(page): checks below to something like this:
-# @controller.registry.action('Do something with a Google Sheet', page_filter=is_google_sheet)
-# async def some_google_sheet_action(browser: BrowserContext):
-# ...
+ return page.url.startswith('https://docs.google.com/spreadsheets/')
@controller.registry.action('Google Sheets: Open a specific Google Sheet')
diff --git a/tests/test_action_filters.py b/tests/test_action_filters.py
index cb2b565b3..4b8d12a90 100644
--- a/tests/test_action_filters.py
+++ b/tests/test_action_filters.py
@@ -198,12 +198,20 @@ class TestActionFilters:
registry.actions = actions
- # Check that all matching filters are included
+ # Check that only actions with matching filters are included
description = registry.get_prompt_description(mock_page)
- assert 'domain_only' in description
- assert 'page_only' in description
- assert 'both_matching' in description
- assert 'both_one_fail' not in description # Should not match because domain filter fails
+ assert 'domain_only' in description # Domain matches
+ assert 'page_only' in description # Page filter matches
+ assert 'both_matching' in description # Both filters match
+ assert 'both_one_fail' not in description # Domain filter fails
+
+ # Test with different URL where page filter fails
+ mock_page.url = 'https://example.com/dashboard'
+ description = registry.get_prompt_description(mock_page)
+ assert 'domain_only' in description # Domain matches
+ assert 'page_only' not in description # Page filter fails
+ assert 'both_matching' not in description # Page filter fails
+ assert 'both_one_fail' not in description # Domain filter fails
@pytest.mark.asyncio
async def test_registry_action_decorator(self):
@@ -259,13 +267,18 @@ class TestActionFilters:
def page_filter_action():
pass
+ @registry.action(description='Both filters action', domains=['example.com'], page_filter=lambda page: 'admin' in page.url)
+ def both_filters_action():
+ pass
+
# Initial action model should only include no_filter_action
initial_model = registry.create_action_model()
assert 'no_filter_action' in initial_model.model_fields
assert 'domain_filter_action' not in initial_model.model_fields
assert 'page_filter_action' not in initial_model.model_fields
+ assert 'both_filters_action' not in initial_model.model_fields
- # Action model with page should include matching actions
+ # Action model with matching page should include all matching actions
mock_page = MagicMock(spec=Page)
mock_page.url = 'https://example.com/admin'
@@ -273,10 +286,20 @@ class TestActionFilters:
assert 'no_filter_action' in page_model.model_fields
assert 'domain_filter_action' in page_model.model_fields
assert 'page_filter_action' in page_model.model_fields
-
- # Action model with non-matching page should only include no_filter_action
- mock_page.url = 'https://other.com/page'
- non_matching_model = registry.create_action_model(page=mock_page)
- assert 'no_filter_action' in non_matching_model.model_fields
- assert 'domain_filter_action' not in non_matching_model.model_fields
- assert 'page_filter_action' not in non_matching_model.model_fields
+ assert 'both_filters_action' in page_model.model_fields
+
+ # Action model with non-matching domain should exclude domain-filtered actions
+ mock_page.url = 'https://other.com/admin'
+ non_matching_domain_model = registry.create_action_model(page=mock_page)
+ assert 'no_filter_action' in non_matching_domain_model.model_fields
+ assert 'domain_filter_action' not in non_matching_domain_model.model_fields
+ assert 'page_filter_action' in non_matching_domain_model.model_fields
+ assert 'both_filters_action' not in non_matching_domain_model.model_fields
+
+ # Action model with non-matching page filter should exclude page-filtered actions
+ mock_page.url = 'https://example.com/dashboard'
+ non_matching_page_model = registry.create_action_model(page=mock_page)
+ assert 'no_filter_action' in non_matching_page_model.model_fields
+ assert 'domain_filter_action' in non_matching_page_model.model_fields
+ assert 'page_filter_action' not in non_matching_page_model.model_fields
+ assert 'both_filters_action' not in non_matching_page_model.model_fields