【问题标题】:How to avoid 'This type of file can harm your computer' pop up in chrome autoamtion using selenium如何避免使用 selenium 在 chrome 自动化中弹出“这种类型的文件可能会损害您的计算机”
【发布时间】:2016-10-04 05:49:20
【问题描述】:

我正在使用 selenium 自动填充 chrome 的网站。当我下载 exe 或 XML 文件时,我会弹出“这种类型的文件可能会损害您的计算机”,并带有保留和丢弃选项。如何以编程方式禁用此功能? 我在 c# 中实现这个, 我试过了,

chromeOptions.AddUserProfilePreference("disable-popup-blocking", "true") 

但这对我不起作用。 如何禁用此弹出窗口? 如果不可能,如何接受警告? 请帮帮我。

【问题讨论】:

  • 可能您需要的是chromeOptions.AddUserProfilePreference("disable-popup-blocking", "false"),而不是阻止弹出窗口。

标签: c# google-chrome selenium selenium-chromedriver webautomation


【解决方案1】:

要禁用消息,您需要将首选项safebrowsing.enabled 设置为true。这是一个使用 CSharp 的工作示例:

var options = new ChromeOptions();
options.AddUserProfilePreference("download.default_directory", "C:\\Downloads");
options.AddUserProfilePreference("download.prompt_for_download", false);
options.AddUserProfilePreference("download.directory_upgrade", true);
options.AddUserProfilePreference("safebrowsing.enabled", true);

var driver = new ChromeDriver(options);
driver.Navigate().GoToUrl("http://www.7-zip.org/a/7z1602.exe");

对于偏好的描述:

https://chromium.googlesource.com/chromium/src/+/master/chrome/common/pref_names.cc

【讨论】:

    【解决方案2】:

    也许你需要的是改变

    chromeOptions.AddUserProfilePreference("disable-popup-blocking", "true")     // disables blocking the popup
    

    chromeOptions.AddUserProfilePreference("disable-popup-blocking", "false")   // enables blocking the popup
    

    而是避免弹出窗口。

    编辑:如果您被有害的文件内容卡住,您可以尝试使用 experimental chrome options 设置:

    Map<String, Object> prefs = new HashMap<String, Object>();
    prefs.put("safebrowsing.enabled", "true");
    chromeOptions.setExperimentalOption("prefs", prefs);
    

    引用文档here 中的示例:

    设置 Chrome 偏好设置

    ChromeOptions options = new ChromeOptions(); 
    Map<String, Object> prefs = new HashMap<String, Object>(); 
    prefs.put("profile.default_content_settings.popups", 0);
    options.setExperimentalOption("prefs", prefs);
    

    日期 - 2016 年 6 月 4 日 [C#]

    浏览了几个链接,可以发现.Net 仍然没有setExperimentalOption 首选项以及ChromeOptions。因此,使用here 列出的标志的Add Argument to the ChromeOptions using C# 的一种方法是:

    chromeOptions.AddArgument("--safebrowsing-disable-download-protection");
    

    禁用检查下载 url 和下载的安全浏览功能 内容的哈希以确保内容不是恶意的。

    日期 - 2016 年 6 月 4 日 [JAVA]

    文档here 引用为:

    public void setExperimentalOption(java.lang.String name, java.lang.Object 值)

    设置实验选项。对新的 ChromeDriver 选项有用还没有 通过 ChromeOptions API 公开

    【讨论】:

    • 这是一个古老的问题 - 双重否定! :)
    • @kyle_engineer 我知道它是一个旧问题,但建议的解决方案都不起作用。你能回答我的问题吗?
    • 抱歉@ITresearcher,我没有任何以编程方式影响 Chrome 的经验,也不知道设置。但我想谷歌不会很容易抑制对潜在恶意软件的警告。这接近于安全性,通常浏览器不允许站点直接影响这些。用户交互的全部意义在于验证风险。
    • 好的。我认为这可以通过其他方式实现。有许多开发人员使用 selenium 和 chrome 驱动程序。
    • 'setExperimentalOption' 不是 'OpenQA.Selenium.Chrome.ChromeOptions' 的成员。我收到此错误
    猜你喜欢
    • 2019-08-19
    • 2011-09-10
    • 1970-01-01
    • 2020-10-30
    • 1970-01-01
    • 1970-01-01
    • 2018-07-19
    相关资源
    最近更新 更多