From 37c9652e6d24aa3d1f5e196c41d6518203a5e149 Mon Sep 17 00:00:00 2001 From: octo-patch Date: Wed, 8 Apr 2026 10:36:48 +0800 Subject: [PATCH] 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. --- sources/browser.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/sources/browser.py b/sources/browser.py index a4b1c0d..02e58be 100644 --- a/sources/browser.py +++ b/sources/browser.py @@ -21,6 +21,7 @@ import random import os import shutil import uuid +import socket import tempfile import markdownify 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") 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: """Create Chrome options - separated for reusability.""" 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-background-timer-throttling") 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-backgrounding-occluded-windows') chrome_options.add_argument('--disable-renderer-backgrounding')