import os, subprocess, sys, time, urllib.request, json

chrome_paths = [
    r"C:\Program Files\Google\Chrome\Application\chrome.exe",
    r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe",
]
chrome = None
for p in chrome_paths:
    exists = os.path.exists(p)
    print(f"{'EXISTS' if exists else 'MISSING'}: {p}")
    if exists:
        chrome = p

if not chrome:
    print("Chrome not found!")
    sys.exit(1)

USER_DATA_DIR = r"C:\ChromeDebug_BJH"
PORT = 9223

cmd = [
    chrome,
    f'--remote-debugging-port={PORT}',
    f'--user-data-dir={USER_DATA_DIR}',
    '--no-first-run',
    '--no-default-browser-check',
    '--new-window',
    'https://baijiahao.baidu.com',
]

print(f"Running: {' '.join(cmd)}")

# Try with shell=True + start command
try:
    result = subprocess.run(
        f'start "" "{chrome}" --remote-debugging-port={PORT} --user-data-dir="{USER_DATA_DIR}" --no-first-run --no-default-browser-check --new-window https://baijiahao.baidu.com',
        shell=True,
        capture_output=True,
        text=True
    )
    print(f"shell result: {result.returncode}")
    print(result.stdout)
    print(result.stderr)
except Exception as e:
    print(f"Error: {e}")
