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: keep balanced trailing brackets in URLs extracted from task text
Aniket Waghd33cc68f06d5
diff --git a/browser_use/utils.py b/browser_use/utils.pyindex 03e651d38..eb1814123 100644--- a/browser_use/utils.py+++ b/browser_use/utils.py@@ -39,6 +39,10 @@ def is_placeholder_url(url: str) -> bool:return len(labels) >= 2 and all(re.fullmatch(r'x+', label) for label in labels)+_TRAILING_PROSE_PUNCTUATION = frozenset('.,;:!?([')+_CLOSING_TO_OPENING_BRACKET = {')': '(', ']': '['}++def sanitize_url_candidate(url: str) -> str:"""Normalize a URL candidate captured from prose before auto-navigation."""candidate = url.strip()@@ -46,7 +50,22 @@ def sanitize_url_candidate(url: str) -> str:# "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)++ # Strip trailing prose punctuation, but keep a closing bracket the URL opened+ # itself, e.g. /wiki/Python_(programming_language). A closing bracket is only+ # prose when it has no opener inside the candidate, as in "(see https://x.com/a)".+ while candidate:+ last_char = candidate[-1]+ if last_char in _TRAILING_PROSE_PUNCTUATION:+ candidate = candidate[:-1]+ continue+ opening_bracket = _CLOSING_TO_OPENING_BRACKET.get(last_char)+ if opening_bracket is not None and candidate.count(last_char) > candidate.count(opening_bracket):+ candidate = candidate[:-1]+ continue+ break++ return candidate# Lazy import for error typesdiff --git a/tests/ci/test_beta_agent.py b/tests/ci/test_beta_agent.pyindex 82aef6f9c..29f638856 100644--- a/tests/ci/test_beta_agent.py+++ b/tests/ci/test_beta_agent.py@@ -4305,6 +4305,14 @@ def test_beta_agent_exposes_task_helper_methods():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'+ # A closing bracket the URL opened itself is part of the path, not prose.+ wikipedia_task = 'Summarize https://en.wikipedia.org/wiki/Python_(programming_language)'+ assert agent._extract_start_url(wikipedia_task) == 'https://en.wikipedia.org/wiki/Python_(programming_language)'+ assert browser_use_agent._extract_start_url(wikipedia_task) == 'https://en.wikipedia.org/wiki/Python_(programming_language)'+ assert agent._extract_start_url('Check https://example.com/a[1] please') == 'https://example.com/a[1]'+ # ...but one the prose opened is still dropped.+ assert agent._extract_start_url('See the docs (https://example.com/guide) for details.') == 'https://example.com/guide'+def test_beta_agent_exposes_url_text_helper_methods():from browser_use.beta import Agent