mirror of
https://github.com/NanmiCoder/MediaCrawler.git
synced 2026-07-28 15:30:25 +08:00
fix: 修复已有浏览器 CDP 连接
This commit is contained in:
@@ -316,16 +316,29 @@ class CDPBrowserManager:
|
||||
"""
|
||||
try:
|
||||
if config.CDP_CONNECT_EXISTING:
|
||||
# Existing Chrome exposes the browser websocket URL from /json/version.
|
||||
# Newer Chrome versions return a UUID path instead of plain /devtools/browser.
|
||||
ws_url = await self._get_browser_websocket_url(self.debug_port)
|
||||
# Existing browser remote debugging in Chrome 136+ does not expose
|
||||
# /json/version. Connect directly and wait for user confirmation.
|
||||
ws_url = f"ws://localhost:{self.debug_port}/devtools/browser"
|
||||
utils.logger.info(f"[CDPBrowserManager] Connecting to existing browser via CDP: {ws_url}")
|
||||
utils.logger.info(
|
||||
"[CDPBrowserManager] Please check your browser for a confirmation dialog and accept it"
|
||||
)
|
||||
self.browser = await playwright.chromium.connect_over_cdp(
|
||||
ws_url, timeout=config.BROWSER_LAUNCH_TIMEOUT * 1000
|
||||
)
|
||||
try:
|
||||
self.browser = await playwright.chromium.connect_over_cdp(
|
||||
ws_url, timeout=config.BROWSER_LAUNCH_TIMEOUT * 1000
|
||||
)
|
||||
except Exception as direct_error:
|
||||
utils.logger.warning(
|
||||
"[CDPBrowserManager] Direct existing-browser CDP connection failed: "
|
||||
f"{direct_error}. Trying /json/version discovery..."
|
||||
)
|
||||
ws_url = await self._get_browser_websocket_url(self.debug_port)
|
||||
utils.logger.info(
|
||||
f"[CDPBrowserManager] Connecting to existing browser via discovered CDP: {ws_url}"
|
||||
)
|
||||
self.browser = await playwright.chromium.connect_over_cdp(
|
||||
ws_url, timeout=config.BROWSER_LAUNCH_TIMEOUT * 1000
|
||||
)
|
||||
else:
|
||||
# For launched browser, get WebSocket URL first
|
||||
ws_url = await self._get_browser_websocket_url(self.debug_port)
|
||||
|
||||
Reference in New Issue
Block a user