Selenium Python启动Chrome指定配置文件失败求助
问题:Selenium启动Chrome指定配置文件失败,报错DevToolsActivePort不存在
尝试用以下Python代码打开Chrome指定配置文件,Chrome能启动并加载配置,但程序直接退出,无法打开目标网站,终端报错如下。
代码示例
from selenium import webdriver from selenium.webdriver.chrome.options import Options #create chromeoptions instance options = webdriver.ChromeOptions() #provide location where chrome stores profiles options.add_argument(r"--user-data-dir=C:\Users\{user name}\AppData\Local\Google\Chrome\User Data") options.add_argument(r"--profile-directory=Profile 2") #specify where your chrome driver present in your pc driver = webdriver.Chrome(options=options) #provide website url here driver.get("https://www.google.co.in")
终端报错输出
Opening in existing browser session. Traceback (most recent call last): File "C:\Users\path\bot2.py", line 11, in <module> driver = webdriver.Chrome(options=options) File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\chrome\webdriver.py", line 81, in __init__ super().__init__( File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\chromium\webdriver.py", line 106, in __init__ super().__init__( File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\remote\webdriver.py", line 288, in __init__ self.start_session(capabilities, browser_profile) File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\remote\webdriver.py", line 381, in start_session response = self.execute(Command.NEW_SESSION, parameters) File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\remote\webdriver.py", line 444, in execute self.error_handler.check_response(response) File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\remote\errorhandler.py", line 249, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited normally. (unknown error: DevToolsActivePort file doesn't exist) (The process started from chrome location C:\Users\user\AppData\Local\Google\Chrome\Application\chrome.exe is no longer running, so ChromeDriver is assuming that Chrome has crashed.) Stacktrace: Backtrace: (No symbol) [0x00A86643] (No symbol) [0x00A1BE21] (No symbol) [0x0091DA9D] (No symbol) [0x0093F2A7] (No symbol) [0x0093A899] (No symbol) [0x00976917] (No symbol) [0x0097655C] (No symbol) [0x0096FB76] (No symbol) [0x009449C1] (No symbol) [0x00945E5D] GetHandleVerifier [0x00CFA142+2497106] GetHandleVerifier [0x00D285D3+2686691] GetHandleVerifier [0x00D2BB9C+2700460] GetHandleVerifier [0x00B33B10+635936] (No symbol) [0x00A24A1F] (No symbol) [0x00A2A418] (No symbol) [0x00A2A505] (No symbol) [0x00A3508B] BaseThreadInitThunk [0x753800F9+25] RtlGetAppContainerNamedObjectPath [0x77507BBE+286] RtlGetAppContainerNamedObjectPath [0x77507B8E+238]
解决方法
关闭所有Chrome进程
报错中的Opening in existing browser session表明已有Chrome实例在运行,Selenium无法接管现有会话。打开任务管理器,结束所有chrome.exe进程后重新运行脚本。添加必要的Chrome启动参数
给ChromeOptions补充以下参数,解决端口冲突和启动干扰问题:from selenium import webdriver options = webdriver.ChromeOptions() # 指定配置文件路径 options.add_argument(r"--user-data-dir=C:\Users\{user name}\AppData\Local\Google\Chrome\User Data") options.add_argument(r"--profile-directory=Profile 2") # 添加关键参数 options.add_argument("--no-first-run") options.add_argument("--no-default-browser-check") options.add_argument("--remote-debugging-port=9222") driver = webdriver.Chrome(options=options) driver.get("https://www.google.co.in")验证ChromeDriver与Chrome版本匹配
检查Chrome浏览器版本,下载对应版本的ChromeDriver,版本不匹配会导致启动失败。复制配置文件到独立路径(可选)
如果上述方法无效,复制一份Chrome用户数据目录到新路径(如C:\Chrome_Profiles\Profile 2),修改--user-data-dir指向该路径,避免与本地正在使用的Chrome实例冲突。
内容的提问来源于stack exchange,提问作者Shaheen
相关产品推荐
相关产品推荐

