Skip to content

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

fix: handle file:// urls in _extract_start_url

Laith Weinberger62df6b11f948
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.py
index c15080d94..eedf3551b 100644
--- a/browser_use/agent/service.py
+++ b/browser_use/agent/service.py
@@ -2288,7 +2288,7 @@ class Agent(Generic[Context, AgentStructuredOutput]):
# Look for common URL patterns
patterns = [
- r'https?://[^\s<>"\']+', # Full URLs with http/https
+ r'(?:https?|file)://[^\s<>"\']+', # Full URLs
r'(?:www\.)?[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*\.[a-zA-Z]{2,}(?:/[^\s<>"\']*)?', # Domain names with subdomains and optional paths
]
@@ -2373,12 +2373,17 @@ class Agent(Generic[Context, AgentStructuredOutput]):
}
found_urls = []
+ matched_spans: list[tuple[int, int]] = []
for pattern in patterns:
matches = re.finditer(pattern, task_without_emails)
for match in matches:
url = match.group(0)
original_position = match.start() # Store original position before URL modification
+ # Skip fragments of URLs already matched by earlier pattern if any(match.start() < end and match.end() > start for start, end in matched_spans):
+ continue
+ matched_spans.append((match.start(), match.end()))
+
# Remove trailing punctuation that's not part of URLs
url = sanitize_url_candidate(url)
@@ -2386,13 +2391,18 @@ class Agent(Generic[Context, AgentStructuredOutput]):
self.logger.debug(f'Excluding placeholder URL from auto-navigation: {url}')
continue
- # Check if URL ends with a file extension that should be excluded
url_lower = url.lower()
+ has_scheme = url_lower.startswith(('http://', 'https://', 'file://'))
+
+ # Check if URL ends with file extension
should_exclude = False
- for ext in excluded_extensions:
- if f'.{ext}' in url_lower:
+ if not url_lower.startswith('file://'):
+ for ext in excluded_extensions:
+ if f'.{ext}' in url_lower:
+ should_exclude = True
+ break
+ if not has_scheme and '.htm' in url_lower:
should_exclude = True
- break
if should_exclude:
self.logger.debug(f'Excluding URL with file extension from auto-navigation: {url}')
@@ -2408,7 +2418,7 @@ class Agent(Generic[Context, AgentStructuredOutput]):
continue
# Add https:// if missing (after excluded words check to avoid position calculation issues)
- if not url.startswith(('http://', 'https://')):
+ if not has_scheme:
url = 'https://' + url
found_urls.append(url)
diff --git a/browser_use/beta/service.py b/browser_use/beta/service.py
index f7a20f134..4664a539d 100644
--- a/browser_use/beta/service.py
+++ b/browser_use/beta/service.py
@@ -1397,7 +1397,7 @@ def _task_with_sensitive_data_context(task: str, sensitive_context: dict[str, An
def _extract_start_url(task: str) -> str | None:
task_without_emails = re.sub(r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b', '', task)
patterns = [
- r'https?://[^\s<>"\']+',
+ r'(?:https?|file)://[^\s<>"\']+',
r'(?:www\.)?[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*\.[a-zA-Z]{2,}(?:/[^\s<>"\']*)?',
]
excluded_extensions = {
@@ -1463,19 +1463,29 @@ def _extract_start_url(task: str) -> str | None:
excluded_words = {'never', 'dont', 'not', "don't"}
found_urls = []
+ matched_spans: list[tuple[int, int]] = []
for pattern in patterns:
for match in re.finditer(pattern, task_without_emails):
+ # Skip fragments of URLs already matched by an earlier pattern
+ if any(match.start() < end and match.end() > start for start, end in matched_spans):
+ continue
+ matched_spans.append((match.start(), match.end()))
url = sanitize_url_candidate(match.group(0))
url_lower = url.lower()
+ has_scheme = url_lower.startswith(('http://', 'https://', 'file://'))
if is_placeholder_url(url):
continue
- if any(f'.{ext}' in url_lower for ext in excluded_extensions):
- continue
+ # file:// URLs are explicit navigation targets, keep them as-is
+ if not url_lower.startswith('file://'):
+ if any(f'.{ext}' in url_lower for ext in excluded_extensions):
+ continue
+ if not has_scheme and '.htm' in url_lower:
+ continue
context_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):
continue
- if not url.startswith(('http://', 'https://')):
+ if not has_scheme:
url = 'https://' + url
found_urls.append(url)