Selenium指定user-data-dir启动Chrome时崩溃问题求助
问题:启动带已登录谷歌账号的Selenium Chrome浏览器崩溃
我尝试启动已登录特定谷歌账号的Selenium Chrome浏览器,但程序崩溃,报错提示Chrome启动失败。
我的代码:
let option = new chrome.Options(); option.setChromeBinaryPath("C:/Program Files/Google/Chrome/Application/chrome.exe") option.addArguments("user-data-dir=C:/Users/adria/AppData/Local/Google/Chrome/User Data/") option.addArguments("profile-directory=Profile 8") const driver = new webdriver.Builder("chromedriver.exe") .forBrowser('chrome') .setChromeOptions(option) .build()
错误信息:
C:\Users\adria\Desktop\project\node_modules\selenium-webdriver\lib\error.js:524 let err = new ctor(data.message) ^ WebDriverError: unknown error: Chrome failed to start: exited normally. (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.) at Object.throwDecodedError (C:\Users\adria\Desktop\project\node_modules\selenium-webdriver\lib\error.js:524:15) at parseHttpResponse (C:\Users\adria\Desktop\project\node_modules\selenium-webdriver\lib\http.js:601:13) at Executor.execute (C:\Users\adria\Desktop\project\node_modules\selenium-webdriver\lib\http.js:529:28) at processTicksAndRejections (node:internal/process/task_queues:96:5) { remoteStacktrace: 'Backtrace:\n' + '\tGetHandleVerifier [0x006CA813+48355]\n' + '\t(No symbol) [0x00654FF7]\n' + '\tBaseThreadInitThunk [0x77047D59+25]\n' + '\tRtlInitializeExceptionChain [0x77DDB74B+107]\n' + '\tRtlClearBits [0x77DDB6CF+191]\n' }
解决方案
- 关闭所有Chrome进程:如果已有Chrome实例在使用指定的用户数据目录,Selenium无法获取该目录的独占使用权,会导致启动失败。打开任务管理器,结束所有
chrome.exe进程后重新运行程序。 - 修正路径格式:Windows系统下路径建议使用转义反斜杠,避免路径解析错误:
option.addArguments("user-data-dir=C:\\Users\\adria\\AppData\\Local\\Google\\Chrome\\User Data") - 添加必要的启动参数:补充以下参数解决DevToolsActivePort问题:
option.addArguments("--no-sandbox") option.addArguments("--disable-dev-shm-usage") option.addArguments("--remote-debugging-port=9222") - 匹配ChromeDriver与Chrome版本:确保你使用的ChromeDriver版本和本地安装的Chrome浏览器版本完全一致,版本不兼容会导致启动异常。
- 检查目录权限:确认运行程序的用户对
User Data目录有读写权限,权限不足会导致无法加载用户配置。
修改后的示例代码
let option = new chrome.Options(); option.setChromeBinaryPath("C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe") option.addArguments("user-data-dir=C:\\Users\\adria\\AppData\\Local\\Google\\Chrome\\User Data") option.addArguments("profile-directory=Profile 8") // 添加解决DevToolsActivePort的参数 option.addArguments("--no-sandbox") option.addArguments("--disable-dev-shm-usage") option.addArguments("--remote-debugging-port=9222") const driver = new webdriver.Builder() .forBrowser('chrome') .setChromeOptions(option) .build()
内容的提问来源于stack exchange,提问作者nevo_e
相关产品推荐
相关产品推荐

