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
Sanitize direct-start URL candidates
Magnus Müller91705e22b778
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.pyindex 43bebc780..4d18660ab 100644--- a/browser_use/agent/service.py+++ b/browser_use/agent/service.py@@ -70,6 +70,7 @@ from browser_use.utils import (get_browser_use_version,get_git_info,is_placeholder_url,+ sanitize_url_candidate,time_execution_async,time_execution_sync,)@@ -1505,7 +1506,7 @@ class Agent(Generic[Context, AgentStructuredOutput]):original_position = match.start() # Store original position before URL modification# Remove trailing punctuation that's not part of URLs- url = re.sub(r'[.,;:!?()\[\]]+$', '', url)+ url = sanitize_url_candidate(url)if is_placeholder_url(url):self.logger.debug(f'Excluding placeholder URL from auto-navigation: {url}')diff --git a/browser_use/rust/service.py b/browser_use/rust/service.pyindex 54107bd11..592db0de6 100644--- a/browser_use/rust/service.py+++ b/browser_use/rust/service.py@@ -67,6 +67,7 @@ from browser_use.utils import (get_browser_use_version,get_git_info,is_placeholder_url,+ sanitize_url_candidate,)@@ -866,7 +867,7 @@ def _extract_start_url(task: str) -> str | None:found_urls = []for pattern in patterns:for match in re.finditer(pattern, task_without_emails):- url = re.sub(r'[.,;:!?()\[\]]+$', '', match.group(0))+ url = sanitize_url_candidate(match.group(0))url_lower = url.lower()if is_placeholder_url(url):continuediff --git a/browser_use/utils.py b/browser_use/utils.pyindex d822ae5e3..5e878bc9a 100644--- a/browser_use/utils.py+++ b/browser_use/utils.py@@ -38,6 +38,17 @@ def is_placeholder_url(url: str) -> bool:return len(labels) >= 2 and all(re.fullmatch(r'x+', label) for label in labels)++def sanitize_url_candidate(url: str) -> str:+ """Normalize a URL candidate captured from prose before auto-navigation."""+ candidate = url.strip()+ # Some benchmark tasks arrive with escaped newlines in prose, e.g.+ # "https://example.com/search.\\n2. Next step". Those are task text,+ # not part of the URL.+ candidate = re.split(r'\\[nrt]', candidate, maxsplit=1)[0]+ return re.sub(r'[.,;:!?()\[\]]+$', '', candidate)++# Import error types - these may need to be adjusted based on actual import pathstry:from openai import BadRequestError as OpenAIBadRequestErrordiff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.pyindex 3b7ae3e19..aa55fa58b 100644--- a/tests/ci/test_rust_agent.py+++ b/tests/ci/test_rust_agent.py@@ -2747,6 +2747,12 @@ def test_rust_agent_exposes_task_helper_methods():assert 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 Noneassert browser_use_agent._extract_start_url('Use https://XXX.XX as a placeholder in the table.') is None+ numbered_task = (+ '1. Navigate to https://elibrary.ferc.gov/eLibrary/search.\\n'+ '2. Ensure "General Search" is selected.'+ )+ assert agent._extract_start_url(numbered_task) == 'https://elibrary.ferc.gov/eLibrary/search'+ assert browser_use_agent._extract_start_url(numbered_task) == 'https://elibrary.ferc.gov/eLibrary/search'def test_rust_agent_exposes_url_text_helper_methods():