使用brew更新ChromeDriver仍不匹配Chrome版本,Rselenium运行报错求助
问题重现
运行Rselenium代码时出现如下错误:
Selenium message:session not created: This version of ChromeDriver only supports Chrome version 106
Current browser version is 111.0.5563.64 with binary path /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
Build info: version: '4.0.0-alpha-2', revision: 'f148142cf8', time: '2019-07-01T21:30:10'
System info: host: 'MacBook-Air.local', ip: 'fe80:0:0:0:caa:f7db:27fc:1860%en0', os.name: 'Mac OS X', os.arch: 'aarch64', os.version: '13.2.1', java.version: '19.0.2'
Driver info: driver.version: unknown
已尝试brew更新ChromeDriver,但代码仍调用旧版本驱动。
解决建议
确认当前ChromeDriver的版本和路径
打开终端执行以下命令:chromedriver --version which chromedriver检查输出的版本是否匹配Chrome 111,以及路径是否为brew安装的路径(通常是
/usr/local/bin/chromedriver)。如果版本不对,说明系统中存在旧驱动且路径优先级更高。手动在代码中指定ChromeDriver路径
在R代码里直接指定brew安装的驱动路径,同时明确Chrome浏览器路径:library(RSelenium) # 先通过brew list chromedriver获取驱动路径,替换下面的driverPath值 driver <- rsDriver( browser = "chrome", driverPath = "/usr/local/bin/chromedriver", extraCapabilities = list( chromeOptions = list( binary = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" ) ) ) remDr <- driver[["client"]]清理系统中的旧ChromeDriver
如果终端查到旧驱动在其他路径(比如/usr/bin/chromedriver),执行命令删除:sudo rm /usr/bin/chromedriver同时确保brew的路径在系统PATH的最前面,执行
echo $PATH检查,/usr/local/bin应位于其他路径之前。使用wdman自动匹配驱动版本
安装并使用wdman包自动下载对应Chrome版本的驱动,避免手动匹配:install.packages("wdman") library(RSelenium) library(wdman) # 自动启动对应版本的ChromeDriver服务 chrome_service <- chrome() # 连接到驱动 remDr <- remoteDriver( browserName = "chrome", port = chrome_service$port ) remDr$open()
内容的提问来源于stack exchange,提问作者TKH

