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

相关推荐
2601_9622982711 小时前
Python与Selenium结合的Web自动化测试全流程实践教程
自动化测试·python·selenium·web测试·pageobjectmodel
2601_9622982718 小时前
【自动化测试】基于Selenium + Python的web自动化框架
自动化测试·python·selenium·测试用例·web框架
2601_962295581 天前
python+selenium实现自动化测试
自动化测试·python·selenium·学习心得·web端测试
B2_Proxy3 天前
Selenium配置代理IP的完整指南:从启动参数到认证扩展
selenium·测试工具
weixin_440730503 天前
Playwright介绍、特点、小例子还有与传统selenium的区别
selenium·测试工具·playwright
2601_962387825 天前
web自动化测试实战教程【selenium/unittest/pytest】【共193课
selenium·pytest·devops·web自动化测试·unittest
2601_962297255 天前
Python、Pytest、Allure、Selenium和Jenkins实现自动化测试集成实例
python·selenium·jenkins·pytest·allure
是吕先森5 天前
【python】selenium实现web自动化测试
前端·python·selenium
weixin_440730505 天前
selenium实战记录-从登陆-首页-进入应用流程用例三种参数化+日志+报告
服务器·selenium·测试工具
岁月失语唯石能言6 天前
《 Web项目测试实战:博客系统的功能/接口/自动化/性能全流程测试报告》
自动化测试·功能测试·selenium·jmeter·接口测试·压力测试·性能测试