使用Python Selenium启动URL时遇chrome not reachable错误求助
我已经查阅过多个类似问题并尝试了所有提出的解决方案,但仍出现相同错误。运行代码后Chrome窗口会打开但URL为空,抛出chrome not reachable异常。
options = webdriver.ChromeOptions() options.add_argument('--disable-infobars') options.binary_location = r"C:\Program Files\Google\Chrome\Application\chrome.exe" options.add_argument('--no-sandbox') options.add_argument('--remote-debugging-port=9222') options.add_argument('--start-maximized') options.add_argument('--disable-web-security') options.add_argument('--disable-webgl') options.add_argument('--disable-popup-blocking') #options.add_argument('--headless') options.add_argument('--disable-setuid-sandbox') options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_experimental_option('excludeSwitches', ['enable-logging']) options.add_experimental_option('useAutomationExtension', False) options.add_argument('--disable-extensions') options.add_argument('--disable-blink-features=AutomationControlled') options.add_argument(f"--user-data-dir=C:\Users\user1\AppData\Local\Google\Chrome\User Data\Default") options.add_argument('--disable-gpu') #pdb.set_trace() #service = Service(r'C:\Data\installs\chromedriver.exe') #service.start() #print(service.service_url) #pdb.set_trace() #driver = webdriver.Remote(service.service_url,options=options) #driver.implicitly_wait(10) driver = webdriver.Chrome(service=Service(executable_path=ChromeDriverManager().install()), options=options) driver.get("https://www.google.com") print("Chrome Driver Loaded")
exception is: Message: chrome not reachable Stacktrace: Backtrace: Ordinal0 [0x0042ACD3+2075859] Ordinal0 [0x003BEE61+1633889] Ordinal0 [0x002BB680+571008] Ordinal0 [0x002B12EA+529130]
在Eclipse中通过F9运行代码,Chrome窗口打开但URL为空,无法正常加载目标页面。
Google Chrome 107.0.5304.88 (Official Build) (64-bit) (cohort: Stable) Chromedriver: 107.0.5304.62
修复用户数据目录路径问题:当前代码用f-string处理路径会导致反斜杠被转义,改用原始字符串格式;同时避免直接使用正在运行的
Default用户目录(Chrome已启动时会引发冲突)。修改示例:options.add_argument(r"--user-data-dir=C:\Users\user1\AppData\Local\Google\Chrome\User Data\TempProfile")注:先复制
Default目录并重命名为TempProfile,使用该副本作为用户数据目录。移除冲突的启动参数:
--remote-debugging-port=9222会让Chrome进入独立调试模式,与WebDriver的连接逻辑冲突,建议移除。同时暂时去掉--disable-web-security、--disable-blink-features=AutomationControlled等非必要参数,简化配置后测试。匹配Chrome与ChromeDriver精确版本:当前Chrome版本为107.0.5304.88,ChromeDriver为107.0.5304.62,小版本差异可能导致兼容性问题,下载与Chrome完全一致的ChromeDriver替换现有版本。
清理残留Chrome进程:打开任务管理器,结束所有
chrome.exe进程后重新运行代码,避免进程冲突导致无法正常连接。逐步排查参数问题:先保留最基础配置(仅设置
binary_location和窗口最大化参数),确认代码正常运行后,再逐个添加其他参数,定位引发异常的具体参数。
内容的提问来源于stack exchange,提问作者nitin srivastava

