Ruby-Selenium环境下禁用Chromium“重新加载页面”弹窗问题求助
解决方案
- 修复版本兼容问题
你当前使用的selenium-webdriver2.53.x为老旧版本,仅适配Chromium 54及更早版本,若你本地运行的Chromium版本高于该范围,会出现大量未定义异常,包括你遇到的页面重载弹窗。可二选一处理:
- 降级Chromium到54.x及以下版本,和现有Selenium版本匹配
- 升级
selenium-webdriver到4.x版本,同时安装对应版本的ChromeDriver匹配你当前的Chromium版本,Gemfile对应修改为:gem 'selenium-webdriver', '~> 4.10'
- 补充启动参数
现有启动参数未覆盖弹窗拦截场景,在你的初始化配置中新增以下参数:
options.add_argument('--disable-notifications') options.add_argument('--noerrdialogs') options.add_argument('--disable-session-crashed-bubble') options.add_argument('--disable-restore-session-state')
- 新增偏好配置
在原有的prefs配置中新增两项内容,避免浏览器误判上次会话异常触发重载提示:
prefs = { download: { 'prompt_for_download' => false, 'default_directory' => 'location' }, plugins: { 'always_open_pdf_externally' => true }, # 新增以下两行 'profile.exit_type' => 'Normal', 'profile.exited_cleanly' => true }
- 启动前清理残留进程
多次运行后残留的Chromium后台进程会导致会话异常,每次初始化Selenium前先清理残留进程:
# Windows环境执行 system('taskkill /f /im chrome.exe 2>nul') # Linux/macOS环境执行 system('pkill -f chromium 2>/dev/null')
内容的提问来源于stack exchange,提问作者BrandoN
相关产品推荐
相关产品推荐

