如何在Python的Selenium自动化中阻止保存地址弹窗
解决Chrome Selenium自动化中地址保存弹窗问题
你当前使用的disable-notifications参数仅用于阻止浏览器通知类弹窗,对Chrome的地址/表单保存提示无效。要禁用这类弹窗,需要通过Chrome的偏好设置或实验性选项配置,以下是两种可行方案:
方案一:通过Chrome偏好设置禁用自动保存
直接在ChromeOptions中添加实验性偏好配置,关闭表单自动保存相关服务:
options = ChromeOptions() # 配置禁用地址、表单保存提示 options.add_experimental_option("prefs", { "profile.default_content_setting_values.autofill": 2, # 禁用自动填充提示 "credentials_enable_service": False, # 关闭凭证保存服务 "profile.password_manager_enabled": False, # 关闭密码管理器 "profile.default_content_settings.popups": 0 # 阻止弹窗类提示 }) # 若需保留通知禁用可保留此参数,无需则删除 options.add_argument("disable-notifications") driver = get_webdriver_for("chrome", options=options) driver.get("https://www.google.com/")
方案二:使用命令行参数关闭表单自动填充
通过Chrome的命令行参数直接禁用表单自动填充功能:
options = ChromeOptions() # 禁用表单自动填充提示 options.add_argument("--disable-form-autofill") # 可选:关闭自动化提示(避免浏览器识别为自动化环境) options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_experimental_option('useAutomationExtension', False) driver = get_webdriver_for("chrome", options=options) driver.get("https://www.google.com/")
补充:页面级禁用(针对特定表单)
如果仅需针对单个页面的表单禁用提示,可在页面加载后通过JavaScript修改表单元素的autocomplete属性:
# 页面加载后执行 driver.execute_script(""" document.querySelectorAll('input, form').forEach(el => { el.setAttribute('autocomplete', 'off'); }); """)
内容的提问来源于stack exchange,提问作者Sherin
相关产品推荐
相关产品推荐

