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
fix(agent): match URL negations as whole words
JayLay03108e880168a941
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.pyindex 3e0970ddd..304f5765a 100644--- a/browser_use/agent/service.py+++ b/browser_use/agent/service.py@@ -77,6 +77,7 @@ from browser_use.utils import (_log_pretty_path,check_latest_browser_use_version,get_browser_use_version,+ has_url_negation,is_placeholder_url,sanitize_url_candidate,time_execution_async,@@ -2367,13 +2368,6 @@ class Agent(Generic[Context, AgentStructuredOutput]):'polynomial',}- excluded_words = {- 'never',- 'dont',- 'not',- "don't",- }-found_urls = []matched_spans: list[tuple[int, int]] = []for pattern in patterns:@@ -2411,16 +2405,14 @@ class Agent(Generic[Context, AgentStructuredOutput]):self.logger.debug(f'Excluding URL with file extension from auto-navigation: {url}')continue- # If in the 20 characters before the url position is a word in excluded_words skip to avoid "Never go to this url"+ # Skip URLs explicitly negated by nearby prose, such as "Never go to this URL".context_start = max(0, original_position - 20)context_text = task_without_emails[context_start:original_position]- if any(word.lower() in context_text.lower() for word in excluded_words):- self.logger.debug(- f'Excluding URL with word in excluded words from auto-navigation: {url} (context: "{context_text.strip()}")'- )+ if has_url_negation(context_text):+ self.logger.debug(f'Excluding negated URL from auto-navigation: {url} (context: "{context_text.strip()}")')continue- # Add https:// if missing (after excluded words check to avoid position calculation issues)+ # Add https:// after the negation check to preserve source positions.if not has_scheme:url = 'https://' + urldiff --git a/browser_use/beta/service.py b/browser_use/beta/service.pyindex d8f116688..3a0bb4c45 100644--- a/browser_use/beta/service.py+++ b/browser_use/beta/service.py@@ -76,6 +76,7 @@ from browser_use.utils import (check_latest_browser_use_version,get_browser_use_version,get_git_info,+ has_url_negation,is_placeholder_url,sanitize_url_candidate,)@@ -1460,8 +1461,6 @@ def _extract_start_url(task: str) -> str | None:'rpm','iso',}- excluded_words = {'never', 'dont', 'not', "don't"}-found_urls = []matched_spans: list[tuple[int, int]] = []for pattern in patterns:@@ -1483,7 +1482,7 @@ def _extract_start_url(task: str) -> str | None:continuecontext_start = max(0, match.start() - 20)context_text = task_without_emails[context_start : match.start()]- if any(word in context_text.lower() for word in excluded_words):+ if has_url_negation(context_text):continueif not has_scheme:url = 'https://' + urldiff --git a/browser_use/utils.py b/browser_use/utils.pyindex 03e651d38..5cbfee760 100644--- a/browser_use/utils.py+++ b/browser_use/utils.py@@ -20,6 +20,7 @@ load_dotenv()# Pre-compiled regex for URL detection - used in URL shorteningURL_PATTERN = re.compile(r'https?://[^\s<>"\']+|www\.[^\s<>"\']+|[^\s<>"\']+\.[a-z]{2,}(?:/[^\s<>"\']*)?', re.IGNORECASE)+URL_NEGATION_PATTERN = re.compile(r"\b(?:never|not|don'?t)\b", re.IGNORECASE)logger = logging.getLogger(__name__)@@ -49,6 +50,11 @@ def sanitize_url_candidate(url: str) -> str:return re.sub(r'[.,;:!?()\[\]]+$', '', candidate)+def has_url_negation(context: str) -> bool:+ """Return whether nearby prose explicitly negates navigation to a URL."""+ return URL_NEGATION_PATTERN.search(context) is not None++# Lazy import for error types# Use sentinel to avoid retrying import when package is not installed_IMPORT_NOT_FOUND: type = type('_ImportNotFound', (), {})diff --git a/tests/ci/test_beta_agent.py b/tests/ci/test_beta_agent.pyindex 82aef6f9c..359f731e9 100644--- a/tests/ci/test_beta_agent.py+++ b/tests/ci/test_beta_agent.py@@ -4297,6 +4297,10 @@ def test_beta_agent_exposes_task_helper_methods():assert 'Expected output format: Answer' in enhancedassert '"answer"' in enhancedassert agent._extract_start_url('Open example.com and report the title.') == 'https://example.com'+ assert agent._extract_start_url('Open this notable site: https://example.com') == 'https://example.com'+ assert browser_use_agent._extract_start_url('Open this notable site: https://example.com') == 'https://example.com'+ assert agent._extract_start_url('Do not open https://example.com.') is None+ assert browser_use_agent._extract_start_url('Do not open https://example.com.') is Noneassert agent._extract_start_url('Email [email protected] only.') is Noneassert agent._extract_start_url('Open https://example.com/report.pdf and summarize it.') is Noneassert agent._extract_start_url('Use https://XXX.XX as a placeholder in the table.') is None