fix: use dynamic free port for remote debugging to prevent SessionNotCreatedException (fixes #312)

The hardcoded --remote-debugging-port=9222 caused SessionNotCreatedException
when port 9222 was already in use by another Chrome instance or process.
Replace with a dynamically allocated free port via socket binding.
This commit is contained in:
octo-patch
2026-04-08 10:36:48 +08:00
parent f8fc43f6d7
commit 37c9652e6d
+8 -1
View File
@@ -21,6 +21,7 @@ import random
import os import os
import shutil import shutil
import uuid import uuid
import socket
import tempfile import tempfile
import markdownify import markdownify
import sys import sys
@@ -153,6 +154,12 @@ def bypass_ssl() -> str:
pretty_print("Bypassing SSL verification issues, we strongly advice you update your certifi SSL certificate.", color="warning") pretty_print("Bypassing SSL verification issues, we strongly advice you update your certifi SSL certificate.", color="warning")
ssl._create_default_https_context = ssl._create_unverified_context ssl._create_default_https_context = ssl._create_unverified_context
def get_free_port() -> int:
"""Find and return a free TCP port on the local machine."""
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(('', 0))
return s.getsockname()[1]
def create_chrome_options(headless=False, stealth_mode=True, crx_path="./crx/nopecha.crx", lang="en") -> Options: def create_chrome_options(headless=False, stealth_mode=True, crx_path="./crx/nopecha.crx", lang="en") -> Options:
"""Create Chrome options - separated for reusability.""" """Create Chrome options - separated for reusability."""
chrome_options = Options() chrome_options = Options()
@@ -179,7 +186,7 @@ def create_chrome_options(headless=False, stealth_mode=True, crx_path="./crx/nop
chrome_options.add_argument("--disable-extensions") chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-background-timer-throttling") chrome_options.add_argument("--disable-background-timer-throttling")
chrome_options.add_argument("--timezone=Europe/Paris") chrome_options.add_argument("--timezone=Europe/Paris")
chrome_options.add_argument('--remote-debugging-port=9222') chrome_options.add_argument(f'--remote-debugging-port={get_free_port()}')
chrome_options.add_argument('--disable-background-timer-throttling') chrome_options.add_argument('--disable-background-timer-throttling')
chrome_options.add_argument('--disable-backgrounding-occluded-windows') chrome_options.add_argument('--disable-backgrounding-occluded-windows')
chrome_options.add_argument('--disable-renderer-backgrounding') chrome_options.add_argument('--disable-renderer-backgrounding')