如何用ChromeDriver处理secure.runescape.com的文件保存授权弹窗?
解决ChromeDriver自动处理文件保存权限弹窗的问题
我来帮你搞定这个困扰!先看看你遇到的弹窗:

你提到的两种方法没用是有原因的:
--disable-notifications是用来禁用通知权限的,和文件保存权限完全不搭边,所以弹窗自然还会出现;- 这个弹窗不是JavaScript生成的
alert,是Chrome浏览器原生的权限请求UI,所以SwitchTo().Alert()根本找不到它,才会报“无弹窗”的错误。
下面给你两个靠谱的解决方案,都是针对Chrome原生权限配置的:
方法一:通过用户偏好设置全局控制文件保存权限
直接在ChromeOptions里配置用户profile的权限偏好,一次性搞定所有站点的文件保存权限请求:
ChromeOptions chromeOptions = new ChromeOptions(); // 允许所有站点请求文件保存权限(不会弹出询问弹窗) chromeOptions.AddUserProfilePreference("profile.default_content_setting_values.automatic_downloads", 1); // 如果想要默认拒绝,把上面的1改成2即可 // chromeOptions.AddUserProfilePreference("profile.default_content_setting_values.automatic_downloads", 2); IWebDriver driver = new ChromeDriver(chromeOptions);
方法二:针对特定站点配置权限
如果你只想给secure.runescape.com单独设置权限,可以用站点级的规则:
ChromeOptions chromeOptions = new ChromeOptions(); // 允许secure.runescape.com的文件保存权限 chromeOptions.AddArgument("--profile-content-settings-defaults=automatic_downloads:1,https://secure.runescape.com:1"); // 拒绝的话把1改成2 // chromeOptions.AddArgument("--profile-content-settings-defaults=automatic_downloads:2,https://secure.runescape.com:2"); IWebDriver driver = new ChromeDriver(chromeOptions);
这两种方法都是直接在Chrome启动时就配置好权限策略,浏览器不会再弹出询问弹窗,完美解决你的问题!
内容的提问来源于stack exchange,提问作者Kyllian Lissens
相关产品推荐
相关产品推荐

