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
normalize missing protocol in url properly
Gregor Žuničcfb1491da5f4
diff --git a/browser_use/browser/session.py b/browser_use/browser/session.pyindex 631243a2d..60c2c3ba0 100644--- a/browser_use/browser/session.py+++ b/browser_use/browser/session.py@@ -19,6 +19,8 @@ from urllib.parse import urlparsefrom browser_use.config import CONFIGfrom browser_use.utils import _log_pretty_path, _log_pretty_url+from .utils import normalize_url+os.environ['PW_TEST_SCREENSHOT_NO_FONTS_READY'] = '1' # https://github.com/microsoft/playwright/issues/35972import anyio@@ -1662,9 +1664,8 @@ class BrowserSession(BaseModel):@require_initializationasync def navigate(self, url: str) -> None:# Add https:// if there's no protocol- normalized_url = url.strip()- if '://' not in normalized_url:- normalized_url = f'https://{normalized_url}'++ normalized_url = normalize_url(url)if self.agent_current_page:await self.agent_current_page.goto(normalized_url, wait_until='domcontentloaded')@@ -2192,9 +2193,8 @@ class BrowserSession(BaseModel):"""Navigate the agent's current tab to a URL"""# Add https:// if there's no protocol- normalized_url = url.strip()- if '://' not in normalized_url:- normalized_url = f'https://{normalized_url}'++ normalized_url = normalize_url(url)if not self._is_url_allowed(normalized_url):raise BrowserError(f'Navigation to non-allowed URL: {normalized_url}')@@ -3048,9 +3048,7 @@ class BrowserSession(BaseModel):# Add https:// if there's no protocolnormalized_url = urlif url:- normalized_url = url.strip()- if '://' not in normalized_url:- normalized_url = f'https://{normalized_url}'+ normalized_url = normalize_url(url)if not self._is_url_allowed(normalized_url):raise BrowserError(f'Cannot create new tab with non-allowed URL: {normalized_url}')diff --git a/browser_use/browser/utils.py b/browser_use/browser/utils.pynew file mode 100644index 000000000..a408c48c1--- /dev/null+++ b/browser_use/browser/utils.py@@ -0,0 +1,38 @@+def normalize_url(url: str) -> str:+ """+ Normalize a URL by adding https:// protocol if needed, while preserving special URLs.++ This function safely adds https:// to URLs that lack a protocol, but preserves+ special URLs like "about:blank", "mailto:...", "tel:...", etc. that should not+ be prefixed with https://.++ Args:+ url: The URL string to normalize++ Returns:+ str: The normalized URL with protocol if needed++ Examples:+ >>> normalize_url('example.com')+ 'https://example.com'+ >>> normalize_url('about:blank')+ 'about:blank'+ >>> normalize_url('mailto:[email protected]')+ 'mailto:[email protected]'+ >>> normalize_url('https://example.com')+ 'https://example.com'+ """+ normalized_url = url.strip()++ # If URL already has a protocol, return as-is+ if '://' in normalized_url:+ return normalized_url++ # Check for special protocols that should not be prefixed with https://+ special_protocols = ['about:', 'mailto:', 'tel:', 'ftp:', 'file:', 'data:', 'javascript:']+ for protocol in special_protocols:+ if normalized_url.startswith(protocol):+ return normalized_url++ # For everything else, add https://+ return f'https://{normalized_url}'