如何使用Selenium Chrome WebDriver处理MacOS上的FaceTime弹窗?
如何使用Selenium Chrome WebDriver处理MacOS上的FaceTime弹窗?
兄弟,我懂你遇到的这个坑!这个FaceTime弹窗是系统级的对话框,根本不在Selenium的管辖范围内——Selenium只能操作浏览器里的网页元素,对这种跳出浏览器的系统弹窗无能为力,所以你之前用Actions发Escape键没用太正常了,因为那按键是发给浏览器的,弹窗根本收不到😅
下面给你两个实用的解决方案,适配你的MacOS+C#+Selenium环境:
方案一:从根源上禁用弹窗(推荐)
直接让Chrome跳过对电话协议(tel://)的询问,这样弹窗根本不会出现。你可以通过设置Chrome的用户偏好来实现:
using OpenQA.Selenium.Chrome; ChromeOptions options = new ChromeOptions(); // 配置Chrome跳过tel协议的外部应用询问 options.AddUserProfilePreference("protocol_handler.excluded_schemes.tel", true); // 初始化WebDriver时传入配置 IWebDriver driver = new ChromeDriver(options);
这个配置会告诉Chrome,对于tel开头的链接,直接不触发外部应用询问,从根源上解决弹窗问题。
方案二:用系统脚本模拟点击取消(如果必须保留询问)
如果业务需要保留弹窗,那只能借助MacOS的AppleScript来模拟系统级的点击操作,然后在C#里调用这个脚本。
第一步:编写AppleScript逻辑
你可以直接把脚本内容嵌入到C#代码中,不需要单独创建文件:
tell application "System Events" -- 确保Chrome处于前台 set frontmost of application "Google Chrome" to true delay 1 -- 给弹窗一点加载时间 -- 检查是否存在目标弹窗 if exists window "Do you want to open FaceTime?" then click button "Cancel" of window "Do you want to open FaceTime?" of application process "Google Chrome" end if end tell
第二步:在C#里调用脚本
用Process类执行MacOS的osascript命令来运行上面的逻辑:
using System.Diagnostics; // 封装关闭弹窗的方法 void CloseFaceTimePopup() { ProcessStartInfo startInfo = new ProcessStartInfo { FileName = "/usr/bin/osascript", Arguments = "-e 'tell application \"System Events\" to set frontmost of application \"Google Chrome\" to true' -e 'delay 1' -e 'tell application \"System Events\" to if exists window \"Do you want to open FaceTime?\" then click button \"Cancel\" of window \"Do you want to open FaceTime?\" of application process \"Google Chrome\" end if'", UseShellExecute = false, RedirectStandardOutput = true, CreateNoWindow = true }; using (Process process = Process.Start(startInfo)) { process.WaitForExit(); } } // 在点击电话链接后调用这个方法 // driver.FindElement(By.LinkText("目标电话链接")).Click(); CloseFaceTimePopup();
注意:运行这个脚本需要给你的测试程序授予“辅助功能”权限(在MacOS系统设置→隐私与安全性→辅助功能里添加你的程序),否则脚本无法模拟系统操作。
备注:内容来源于stack exchange,提问作者ABircan
相关产品推荐
相关产品推荐

