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

Chrome 127+Selenium WebDriver下beforeunload弹窗不显示问题求助

问题解决:Chrome 127+下Selenium无法触发beforeunload弹窗

原因说明

Chrome 127版本开始引入了一项行为变更:当Selenium使用传统HTTP会话而非BiDi(双向通信)会话时,页面的beforeunload提示弹窗会被默认禁用。官方给出的理由是这类提示在自动化场景下可能干扰流程,判定非交互场景下显示无意义。

解决方法

方法1:启用Selenium BiDi会话模式

这是官方推荐的标准方案,通过启用BiDi会话恢复beforeunload弹窗的触发逻辑:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_experimental_option("enable_bidi", True)
driver = webdriver.Chrome(options=options)

# 后续执行页面刷新/导航操作时,即可触发预期的beforeunload弹窗

方法2:添加Chrome启动参数强制启用提示

如果暂时不想切换到BiDi模式,可以通过添加启动参数绕过这个限制:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument("--disable-features=SuppressBeforeUnloadDialogs")
driver = webdriver.Chrome(options=options)

是否需要向Chrome反馈?

不需要。这是Chrome 127版本开始的有意设计变更,相关Chromium和Selenium的公开议题已经明确说明此行为是预期的,官方不会将其视为Bug进行修复。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 05:28:13