You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Selenium Python禁用Microsoft Edge三点菜单中的通知?

Edge自动化测试中通知弹窗干扰测试的解决办法

在Microsoft Edge上执行自动化测试时,浏览器弹出的通知会导致测试上下文转移,进而干扰HTML元素查找,最终引发测试失败。以下是已尝试但无效的配置:

无效配置示例

  • 通过Chromium偏好设置禁用通知和弹窗:
edge_options = EdgeOptions()
edge_options.use_chromium = True
edge_options.add_experimental_option("prefs", {
    "profile.default_content_setting_values.notifications": 2,
    "profile.default_content_setting_values.popups": 2
})
  • 使用命令行参数禁用通知:
edge_options = EdgeOptions()
edge_options.use_chromium = True
edge_options.add_argument('--disable-notifications')

可行解决方案

1. 全面覆盖的偏好设置配置

同时配置多组相关偏好,覆盖更多通知/弹窗触发场景:

edge_options = EdgeOptions()
edge_options.use_chromium = True
edge_options.add_experimental_option("prefs", {
    "profile.default_content_setting_values.notifications": 2,
    "profile.default_content_setting_values.popups": 2,
    "profile.content_settings.exceptions.notifications.*.setting": 2,
    "profile.content_settings.exceptions.popups.*.setting": 2,
    "credentials_enable_service": False,
    "profile.password_manager_enabled": False
})
# 补充弹窗拦截参数
edge_options.add_argument('--disable-popup-blocking')
edge_options.add_argument('--disable-infobars')

2. 主动切换回主浏览器窗口

如果通知无法彻底禁用,可在元素查找前强制切换回主窗口上下文:

# 获取所有窗口句柄
window_handles = driver.window_handles
# 切换至主窗口(默认第一个句柄对应主浏览器窗口)
driver.switch_to.window(window_handles[0])
# 执行元素查找操作
target_element = driver.find_element(By.ID, "target-element-id")

3. Edge专属禁用参数

针对Chromium版Edge,尝试添加更精准的通知禁用参数:

edge_options = EdgeOptions()
edge_options.use_chromium = True
edge_options.add_argument('--disable-notifications')
edge_options.add_argument('--disable-notification-center')
edge_options.add_argument('--disable-desktop-notifications')

内容的提问来源于stack exchange,提问作者Dor Amrani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 11:01:03