Chrome运行时调用ChromeDriver出现DevToolsActivePort不存在错误的求助
问题:Chrome运行时调用ChromeDriver报错DevToolsActivePort不存在
环境:Windows 10(Git-Bash,非WSL),Python + Selenium,Chrome版本114.0.5735.110,ChromeDriver版本114.0.5735.90
此前使用同大版本旧版(Chrome 114.0.5735.16 + 对应ChromeDriver)时,可在Chrome打开默认配置文件的同时,用ChromeDriver调用该配置文件;但升级后出现报错——Chrome处于打开状态时,无论使用相同或不同配置文件都会触发问题,关闭Chrome则代码正常运行。
核心报错信息:
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed. (unknown error: DevToolsActivePort file doesn't exist) (The process started from chrome location C:\Program Files\Google\Chrome\Application\chrome.exe is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
参考代码:
from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options chrome_options = Options() chrome_options.binary_location = r"C:\Program Files\Google\Chrome\Application\chrome.exe" chrome_options.add_argument('--disable-gpu') chrome_options.add_argument(r"--user-data-dir=C:\Users\Issa\AppData\Local\Google\Chrome\User Data") chrome_options.add_argument(r'--profile-directory=Profile 1') chrome_options.add_argument('--headless=new') # 移除"Chrome正被自动化软件控制"提示栏 chrome_options.add_experimental_option("useAutomationExtension", False) chrome_options.add_experimental_option("excludeSwitches",["enable-automation"]) driver_service = Service(r'C:\Users\Issa\Downloads\chromedriver_win32\chromedriver.exe') driver = webdriver.Chrome(service=driver_service, options=chrome_options) driver.get("https://example.com") driver.quit()
解决方法
1. 使用独立的用户数据目录
Chrome的用户数据目录同一时间只能被一个实例占用,升级后权限控制更严格,无法共用。
- 复制现有配置文件(比如
C:\Users\Issa\AppData\Local\Google\Chrome\User Data\Profile 1)到新目录,例如C:\Users\Issa\AppData\Local\Google\Chrome\Automation Profile - 在代码中修改
--user-data-dir和--profile-directory指向新目录:chrome_options.add_argument(r"--user-data-dir=C:\Users\Issa\AppData\Local\Google\Chrome\Automation Profile") chrome_options.add_argument(r'--profile-directory=Default')
2. 指定远程调试端口
添加--remote-debugging-port参数,避免端口冲突:
# 指定一个未被占用的端口,比如9222 chrome_options.add_argument('--remote-debugging-port=9222')
3. 严格匹配Chrome与ChromeDriver版本
当前Chrome版本为114.0.5735.110,ChromeDriver为114.0.5735.90,小版本存在差异,建议下载完全匹配的ChromeDriver版本。
4. 暂时禁用无头模式
无头模式与正常Chrome实例同时运行可能存在兼容性问题,先去掉--headless=new参数测试:
# 注释或删除该行 # chrome_options.add_argument('--headless=new')
修改后的示例代码
from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options chrome_options = Options() chrome_options.binary_location = r"C:\Program Files\Google\Chrome\Application\chrome.exe" chrome_options.add_argument('--disable-gpu') # 使用独立的自动化配置目录 chrome_options.add_argument(r"--user-data-dir=C:\Users\Issa\AppData\Local\Google\Chrome\Automation Profile") chrome_options.add_argument(r'--profile-directory=Default') # 指定远程调试端口 chrome_options.add_argument('--remote-debugging-port=9222') # 移除自动化提示栏 chrome_options.add_experimental_option("useAutomationExtension", False) chrome_options.add_experimental_option("excludeSwitches",["enable-automation"]) driver_service = Service(r'C:\Users\Issa\Downloads\chromedriver_win32\chromedriver.exe') driver = webdriver.Chrome(service=driver_service, options=chrome_options) driver.get("https://example.com") driver.quit()
内容的提问来源于stack exchange,提问作者IssaRice
相关产品推荐
相关产品推荐

