Skip to content

Chrome 127自动测试屏蔽“选择搜索引擎”弹窗指南

问题背景

自从Chrome 127版本更新后,运行Selenium测试时会出现“选择搜索引擎”弹窗,即使已在常规浏览器中设置过。这个弹窗会中断自动化测试流程,需要在ChromeDriver启动配置中禁用此提示。

解决方案原理

通过--disable-search-engine-choice-screen启动参数直接禁用弹窗提示。这是Google官方为测试环境提供的运行时标志,专门用于跳过搜索引擎选择界面。

多语言实现代码示例

以下是主流编程语言的配置实现:

csharp
var options = new ChromeOptions();

// 关键配置:禁用搜索引擎选择屏
options.AddArgument("--disable-search-engine-choice-screen");

// 如需无头模式则添加
// options.AddArgument("--headless=new");

// 初始化驱动
var driver = new ChromeDriver(options);
java
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-search-engine-choice-screen");

// 无头模式配置
// options.addArguments("--headless=new");

WebDriver driver = new ChromeDriver(options);
python
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument("--disable-search-engine-choice-screen")

# 无头模式配置
# chrome_options.add_argument("--headless=new")

driver = webdriver.Chrome(options=chrome_options)
ruby
# 在ActionDispatch::SystemTestCase配置
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
  driven_by :selenium, using: :chrome do |driver_options|
    driver_options.add_argument("--disable-search-engine-choice-screen")
  end
end

注意兼容性

  1. 无头模式特殊处理:同时使用--headless参数时,需保留new参数格式(--headless=new
  2. 参数无效时检查:确认驱动程序版本与Chrome版本匹配(ChromeDriver >= 127)
  3. 全局生效:该配置在每次测试会话中均需添加,不受本地浏览器设置影响

更多实用运行参数

以下常用测试环境参数可配合使用(参考Chrome Flags官方文档):

参数作用
--no-first-run跳过首次运行向导
--disable-infobars禁用信息栏提示
--disable-notifications禁用通知权限请求
--disable-popup-blocking允许所有弹窗

总结

禁用Chrome 127中搜索引擎弹窗的核心是在ChromeOptions中添加--disable-search-engine-choice-screen参数。此解决方案已在多个主流编程语言的Selenium绑定中验证有效,特别适合持续集成环境。建议每次Chrome大版本更新后验证ChromeDriver兼容性,确保自动化测试不受浏览器变更影响。