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

如何用Python/Selenium处理「允许站点用YYY打开XXX链接」弹窗

Python/Selenium处理「允许站点打开外部链接」弹窗方案(Chrome+Firefox)

Chrome 解决方案

A. 取消弹窗

这类弹窗是Chrome原生的外部协议请求弹窗,无法通过常规元素定位操作。两种可行方式:

  • 执行Chrome DevTools命令(CDP)默认拒绝指定协议的请求:
    driver.execute_cdp_cmd("Browser.setPermission", {
        "origin": driver.current_url,
        "permission": "protocol_handler",
        "setting": "denied"
    })
    
  • 若触发弹窗的元素可定位,右键复制链接地址绕过弹窗,直接处理链接内容:
    from selenium.webdriver.common.action_chains import ActionChains
    from selenium.webdriver.common.keys import Keys
    
    element = driver.find_element(By.XPATH, "//触发弹窗的元素路径")
    ActionChains(driver).context_click(element).send_keys(Keys.ARROW_DOWN).send_keys(Keys.ENTER).perform()
    

B. 确认打开链接

  • 加载已手动配置好权限的Chrome用户资料:先手动打开Chrome访问目标站点,允许一次协议打开,再复制个人资料路径(在chrome://version/查看),在代码中加载:
    from selenium import webdriver
    
    options = webdriver.ChromeOptions()
    options.add_argument(r"user-data-dir=C:\Users\你的用户名\AppData\Local\Google\Chrome\User Data\Default") # 替换为你的资料路径
    driver = webdriver.Chrome(options=options)
    
  • 用CDP命令直接授予权限:
    driver.execute_cdp_cmd("Browser.setPermission", {
        "origin": driver.current_url,
        "permission": "protocol_handler",
        "setting": "granted"
    })
    

C. 忽略弹窗的Chrome Options配置

通过prefs参数直接设置默认处理规则,无需弹窗:

  • 默认拒绝指定协议:
    options = webdriver.ChromeOptions()
    options.add_experimental_option("prefs", {
        "protocol_handler.excluded_schemes": {
            "xxx": True # 替换为目标协议,如"tel""mailto"
        }
    })
    driver = webdriver.Chrome(options=options)
    
  • 默认允许指定协议:
    options.add_experimental_option("prefs", {
        "protocol_handler.allowed_schemes": {
            "xxx": True
        }
    })
    

Firefox 解决方案

A. 取消弹窗

通过Firefox Profile配置默认拒绝指定协议的外部请求:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
profile = webdriver.FirefoxProfile()
# 替换xxx为目标协议,设置为false则拒绝外部请求,不弹窗
profile.set_preference("network.protocol-handler.external.xxx", False)
profile.set_preference("network.protocol-handler.ask.xxx", False)
options.profile = profile
driver = webdriver.Firefox(options=options)

B. 确认打开链接

配置Profile默认允许指定协议并绑定外部程序:

profile = webdriver.FirefoxProfile()
profile.set_preference("network.protocol-handler.external.xxx", True)
# 替换为外部程序YYY的完整路径
profile.set_preference("network.protocol-handler.app.xxx", "C:\\Program Files\\YYY\\YYY.exe")
profile.set_preference("network.protocol-handler.ask.xxx", False) # 关闭询问弹窗
options.profile = profile

也可手动在Firefox中允许一次协议打开,然后加载该配置文件。

C. 忽略弹窗的Firefox Profiles配置

通过Profile参数直接设置自动处理逻辑,彻底消除弹窗:

  • 默认拒绝:
    profile.set_preference("network.protocol-handler.ask.xxx", False)
    profile.set_preference("network.protocol-handler.external.xxx", False)
    
  • 默认允许:
    profile.set_preference("network.protocol-handler.ask.xxx", False)
    profile.set_preference("network.protocol-handler.external.xxx", True)
    profile.set_preference("network.protocol-handler.app.xxx", "外部程序路径")
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 01:30:04