RobotFramework中如何用指定旧版ChromeDriver适配旧版Chrome
解决旧版Chrome与ChromeDriver版本不匹配问题(系统无法降级新版Chrome时)
问题核心
你遇到的报错本质是:ChromeDriver 87仅支持Chrome 87,但代码默认调用了系统已安装的Chrome 112。之前的脚本只处理了ChromeDriver的版本下载,未指定要启动的旧版Chrome浏览器路径,导致版本不匹配。
解决步骤
1. 下载指定版本的Chrome浏览器
WebDriverManager仅负责下载ChromeDriver,不包含Chrome浏览器本身,需手动下载Chrome 87.0.4280.88:
- 下载对应系统的安装包(Mac为
.dmg、Windows为.exe) - 安装到自定义路径(避免覆盖系统默认Chrome,比如Mac下
/Applications/Google Chrome 87.app)
2. 编写Python配置脚本:绑定旧版Chrome与匹配的ChromeDriver
创建chrome_87_driver.py,内容如下:
from selenium import webdriver from webdriver_manager.chrome import ChromeDriverManager def get_chrome_87_driver(): # 配置Chrome选项,指定旧版Chrome的二进制路径 options = webdriver.ChromeOptions() # Mac路径示例,Windows请替换为你的旧版Chrome路径,如"C:\\Chrome87\\chrome.exe" options.binary_location = "/Applications/Google Chrome 87.app/Contents/MacOS/Google Chrome" # 自动下载并使用对应版本的ChromeDriver driver_path = ChromeDriverManager(version="87.0.4280.88").install() driver = webdriver.Chrome(executable_path=driver_path, options=options) # 可选:验证版本匹配 print(f"Chrome版本: {driver.capabilities['browserVersion']}") print(f"ChromeDriver版本: {driver.capabilities['chrome']['chromedriverVersion'].split(' ')[0]}") return driver
3. 在RobotFramework中调用配置好的驱动
提供两种调用方式,按需选择:
方式一:直接在Robot脚本中配置参数
*** Settings *** Library SeleniumLibrary *** Variables *** ${URL} https://www.youtube.com/ ${CHROME_87_PATH} /Applications/Google Chrome 87.app/Contents/MacOS/Google Chrome *** Test Cases *** Open YouTube With Chrome87 # 初始化Chrome选项 ${options}= Evaluate sys.modules['selenium.webdriver'].ChromeOptions() sys, selenium.webdriver # 指定旧版Chrome路径 Set Attribute ${options} binary_location ${CHROME_87_PATH} # 自动下载对应ChromeDriver并启动浏览器 Create WebDriver Chrome options=${options} service_args=[--version=87.0.4280.88] Go To ${URL} Maximize Browser Window Close Browser
方式二:调用Python封装的驱动函数
*** Settings *** Library SeleniumLibrary Library ${CURDIR}/chrome_87_driver.py *** Variables *** ${URL} https://www.youtube.com/ *** Test Cases *** Open YouTube With Chrome87 ${driver}= Get Chrome 87 Driver Go To ${URL} Maximize Browser Window Close Browser
关键注意事项
- 旧版Chrome必须安装到非系统默认路径,避免与新版Chrome冲突
- 确保ChromeDriver版本与Chrome版本完全对应(如87.0.4280.88对应Chrome 87系列)
- 若使用无头模式,旧版Chrome需添加参数
--headless=chrome(新版参数--headless=new不兼容旧版)
内容的提问来源于stack exchange,提问作者Gheeno San Pascual III
相关产品推荐
相关产品推荐

