如何禁用 'This type of file can harm your computer' 弹出窗口

How to disable 'This type of file can harm your computer' pop up

我正在使用 selenium chromedriver 来自动化 Web 应用程序。 在我的应用程序中,我需要下载 xml 个文件。但是当我下载 xml 文件时,我会弹出 'This type of file can harm your computer'。我想使用 selenium chromedriver 禁用此弹出窗口,并且我希望始终下载这些类型的文件。如何才能做到这一点?

更新 这是 long standing Chrome bug 从 2012 年开始的。

XML 文件的问题从 Chrome 47.0.2526.80 米开始发生在我身上。 在花了大约 6 个小时尝试关闭所有可能的安全选项后,我尝试了一种不同的方法。

具有讽刺意味的是,打开 Chrome 选项 "Protect you and your device from dangerous sites" 似乎删除了消息 "This type of file can harm your computer. Do you want to keep file.xml anyway?"

我将 'Ruby' 与 'Watir-Webdriver' 一起使用,代码如下所示:

prefs = {
    'safebrowsing' => {
        'enabled' => true,
    }
}

b = Watir::Browser.new :chrome, :prefs => prefs

像这样启动浏览器,启用 safebrowsing 选项,下载 xml 文件而不显示消息警告。 Selenium 和任何编程语言的原理应该是一样的。

##### 编辑时间:2017 年 4 月 13 日

在 Google Chrome 的最新版本中,上述解决方案是不够的。此外,有必要使用以下开关启动浏览器:

--safebrowsing-disable-download-protection

现在,启动浏览器的代码如下所示:

b = Watir::Browser.new :chrome, :prefs => prefs, :switches => %w[--safebrowsing-disable-download-protection]))

我在下面发布了让文件下载对我有用的完整代码: 希望它有帮助 :-) 我正在使用 Java-Selenium

System.setProperty("webdriver.chrome.driver", "C:/chromedriver/chromedriver.exe");
        String downloadFilepath = "D:/MyDeskDownload";
        HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
        chromePrefs.put("profile.default_content_settings.popups", 0);
        chromePrefs.put("download.default_directory", downloadFilepath);
        chromePrefs.put("safebrowsing.enabled", "true"); 
        ChromeOptions options = new ChromeOptions();
        options.setExperimentalOption("prefs", chromePrefs);
        DesiredCapabilities cap = DesiredCapabilities.chrome();
        cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
        cap.setCapability(ChromeOptions.CAPABILITY, options);
        WebDriver driver = new ChromeDriver(cap);

以下 Python 代码适合我

chromeOptions = webdriver.ChromeOptions()
prefs = {'safebrowsing.enabled': 'false'}
chromeOptions.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(chrome_options=chromeOptions)

适合我的 stopped working after a recent update of Chrome. Now you need to use the --safebrowsing-disable-extension-blacklist and --safebrowsing-disable-download-protection command-line switches. This is the WebdriverIO 配置:

var driver = require('webdriverio');
var client = driver.remote({
    desiredCapabilities: {
        browserName: 'chrome',
        chromeOptions: {
            args: [
                'disable-extensions',
                'safebrowsing-disable-extension-blacklist',
                'safebrowsing-disable-download-protection'
            ],
            prefs: {
                'safebrowsing.enabled': true
            }
        }
    }
});

请注意,我还禁用了扩展程序,因为它们通常会干扰自动化测试,但这并不是解决下载 XML 和 JavaScript 文件问题所必需的。

我通过阅读 this list. You can also see them in the Chromium source 找到了这些开关。

唯一对我有用的解决方法:

将参数与 chrome 配置文件的路径一起使用

chromeOptions.add_argument(chrome_profile_path)

在 chrome 配置文件文件夹中搜索文件 download_file_types.pb。 就我而言 ..chromedriver\my_profile\FileTypePolicies\download_file_types.pb

备份此文件,然后使用任何 hexeditor(您可以使用 oline)打开。

搜索您要下载的文件类型,即 xml 并将其更改为任何类型,即 xxx

我在 C# 中使用了所有建议的 chrome 选项,并且仅当我的互联网连接时才对我有效,但当互联网断开连接时 none 它们对我有效。 (我不是 sure.may 是 chrome 安全浏览需要互联网连接)

通过使用旧版本的 chrome(71 版)和 chrome 驱动程序(2.46 版)并在下载后,我看到下载的 XML 文件名包含 '未确认',扩展名为'crdownload' 并且解析 XML 文件将无法正常工作。 最后,用 Thread.Sleep(1000) 创建等待解决了我的问题。

            IWebDriver Driver;

            //chromedriver.exe version2.46 path
            string path = @"C:\cd71";
            ChromeDriverService driverService = ChromeDriverService.CreateDefaultService(path, "chromedriver.exe");
            ChromeOptions options = new ChromeOptions();
            // options.AddArgument("headless");
            options.AddArgument("--window-position=-32000,-32000");
            options.AddUserProfilePreference("download.default_directory", @"c:\xmlFiles");
            options.AddUserProfilePreference("download.prompt_for_download", false);
            options.AddUserProfilePreference("disable-popup-blocking", "true");
            options.AddUserProfilePreference("safebrowsing.enabled", "true");
            Driver = new ChromeDriver(driverService, options);

            try
            {
                 Driver.Navigate().GoToUrl(url);
                 Thread.Sleep(1000);
                 //other works like: XML parse
            }
            catch
            {

            }

我正在使用 Google 版本 80.0.3987.122(官方构建)(32 位)和 ChromeDriver 80.0.3987.106。即使在下载 .xml 文件时添加以下内容后也会出现相同的错误。

$ChromeOptions = New-Object OpenQA.Selenium.Chrome.ChromeOptions
$ChromeOptions.AddArguments(@(
    "--disable-extensions",
    "--ignore-certificate-errors"))

$download = "C:\temp\download"
$ChromeOptions.AddUserProfilePreference("safebrowsing.enabled", "true");
$ChromeOptions.AddUserProfilePreference("download.default_directory", $download);
$ChromeOptions.AddUserProfilePreference("download.prompt_for_download", "false");
$ChromeOptions.AddUserProfilePreference("download.directory_upgrade", "true");

$ChromeDriver = New-Object OpenQA.Selenium.Chrome.ChromeDriver($chromeOptions)

对于上下文,我有一个 .csv,其中包含 .swf 个 Flash 文档及其各自的 URL 列表。由于只有在有效登录后才能访问文件,因此我无法使用基于 requests 的简单解决方案。

就像.xml一样,下载.swf会触发类似的提示。

None 的答案对我有用。

所以我去掉了上面提出的所有额外参数和设置,只创建了 chrome 实例 headless

options.add_argument("--headless")

prefs = {
    "download.default_directory": "C:\LOL\Flash",
    "download.prompt_for_download": False,
    "download.directory_upgrade": True,
}
options.add_experimental_option("prefs",prefs)

我最近遇到了这个,使用 Katalon Studio,Chrome 版本 88。 值得庆幸的是,仅启用安全浏览就可以解决问题。您通过“项目设置”访问设置,然后导航到“所需功能”->“Web UI”->“Chrome”。 如果您没有“prefs”设置,请添加它并将类型设置为 Dictionary。然后在值中添加一个名为“safebrowsing.enabled”的布尔值并将值设置为“true”。 结果可能如下所示: (默认目录设置与本例无关)