Selenium打开外部应用程序的弹窗处理

问题

selenium自动化操作页面跳转到外部应用程序进行下载等操作,各种窗口处理方式无法解决

原因

该窗口属于浏览器窗口,与访问页面无关(已经脱离页面操作层面)

解决

selenium启动浏览器时,对浏览器进行相关窗口设置

解决方式

1、打开应用程序前,打开浏览器开发者模式-network-all

2、触发打开外部应用程序出现窗口,查看红色的报错的请求

3、复制窗口的域名(如图https://apps.apple.com),复制红色报错请求的url协议名称(如图macappstores)

使用如下代码

python 复制代码
chrome_options = webdriver.ChromeOptions()
prefs = {
    "protocol_handler": {
        # 该方法会允许启动,执行中会把本地外部应用唤起。不影响自动化执行,可视化执行时一定程度影响视野
        "allowed_origin_protocol_pairs": {
            # 域名即窗口显示的域名
            # macappstores属于协议名称,从network抓包即可看到。ture表示允许启动,false表示禁止启动(会出现弹窗确认)
            "https://apps.apple.com": {"macappstores": True, "itms-appss": True},
            "https://itunes.apple.com": {"itms-appss": True}
        },
        # 以下方法测试不可用,查找原因需要更新selenium至最新版本(未实验)
        # 该方法理论可以直接不启动应用程序,并且不出现弹窗。最符合实际诉求
        # "excluded_schemes": {
            # ture表示禁止启动,false表示允许启动
            # "https://apps.apple.com": {"macappstores": True},
            # "https://itunes.apple.com": {"itms-appss": True}
        # }
    }
}
chrome_options.add_experimental_option("prefs", prefs)
webdriver.Chrome(driver_path, options=chrome_options)

参考:

https://www.5axxw.com/questions/content/yhke2q

https://blog.csdn.net/qq_26819733/article/details/126799304

相关推荐
mike041213 小时前
Eclipse+maven+selenium自动化测试用例入门
selenium·eclipse·maven
啊巴矲1 天前
小白从零开始勇闯人工智能:爬虫初级篇(Selenium库)
爬虫·selenium·测试工具
尼罗河女娲2 天前
【获取WebSocket】使用 Playwright 监听 Selenium 自动化测试中的 WebSocket 消息(一)
websocket·网络协议·selenium
尼罗河女娲2 天前
【测试开发】Selenium + Chrome 自动化中常用 ChromeOptions 参数说明(实践总结)
chrome·selenium·自动化
爱打代码的小林2 天前
python(爬虫selenium)
爬虫·python·selenium
尼罗河女娲2 天前
【获取WebSocket】使用 Playwright 监听 Selenium 自动化测试中的 WebSocket 消息(二)
websocket·网络协议·selenium
mikejahn2 天前
使用selenium从CNAS网站解析实验室信息
python·selenium·测试工具
Pyeako2 天前
网络爬虫相关操作--selenium库(超详细版)
爬虫·python·selenium
袁袁袁袁满2 天前
基于selenium和亮数据爬取海外电商平台
爬虫·selenium·网络爬虫·数据采集·爬虫实战·电商采集·自动化采集
子夜江寒2 天前
基于Selenium的自动化Web数据采集实践
selenium·自动化