Skip to content

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

fix/cdp_url_parsing

shubham-02173c294f7a384
diff --git a/browser_use/browser/session.py b/browser_use/browser/session.py
index 8584a107a..0d3d1a534 100644
--- a/browser_use/browser/session.py
+++ b/browser_use/browser/session.py
@@ -5,6 +5,7 @@ import logging
from functools import cached_property
from pathlib import Path
from typing import TYPE_CHECKING, Any, Literal, Self, Union, cast, overload
+from urllib.parse import urlunparse
from uuid import UUID
import httpx
@@ -1477,11 +1478,24 @@ class BrowserSession(BaseModel):
self.logger.debug(f'Error stopping old CDP client: {e}')
self._cdp_client_root = None
+ from urllib.parse import urlparse,urlunparse;
+
if not self.cdp_url.startswith('ws'):
# If it's an HTTP URL, fetch the WebSocket URL from /json/version endpoint
- url = self.cdp_url.rstrip('/')
- if not url.endswith('/json/version'):
- url = url + '/json/version'
+ parsed_url = urlparse(self.cdp_url)
+ path = parsed_url.path.rstrip('/')
+
+ if not path.endswith('/json/version'):
+ path = path + '/json/version'
+
+ url = urlunparse((
+ parsed_url.scheme,
+ parsed_url.netloc,
+ path,
+ parsed_url.params,
+ parsed_url.query,
+ parsed_url.fragment
+ ))
# Run a tiny HTTP client to query for the WebSocket URL from the /json/version endpoint
async with httpx.AsyncClient() as client: