How to disable 'This type of file can harm you

2019-01-15 06:38发布

I'm using selenium chromedriver for automating web application. In my application, I need to download xml files. But when I download xml file, I get 'This type of file can harm your computer' pop up. I want to disable this pop up using selenium chromedriver and I want these type of files to be downloaded always. How can this be done? enter image description here

  • Selenium version : 2.47.1
  • Chromedriver version : 2.19

UPDATE it's long standing Chrome bug from 2012.

4条回答
▲ chillily
2楼-- · 2019-01-15 06:51

Following Python code works for me

chromeOptions = webdriver.ChromeOptions()
prefs = {'safebrowsing.enabled': 'false'}
chromeOptions.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(chrome_options=chromeOptions)
查看更多
我想做一个坏孩纸
3楼-- · 2019-01-15 06:58

The accepted answer 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 config that works for me:

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
            }
        }
    }
});

Note that I am also disabling extensions, because they generally interfere with automated testing, but this is not strictly needed to fix the problem with downloading XML and JavaScript files.

I found these switches by reading through this list. You can also see them in the Chromium source.

查看更多
Deceive 欺骗
4楼-- · 2019-01-15 07:01

I am posting below the complete code that got file download working for me: Hope it helps :-) I am using 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);
查看更多
Luminary・发光体
5楼-- · 2019-01-15 07:03

The problem with XML files started to happen to me as of Chrome 47.0.2526.80 m. After spending maybe 6 hours trying to turn off every possible security option I tried a different approach.

Ironically, it seems that turning on the Chrome option "Protect you and your device from dangerous sites" removes the message "This type of file can harm your computer. Do you want to keep file.xml anyway?"

I am using 'Ruby' with 'Watir-Webdriver' where the code looks like this:

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

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

Starting the browser like this, with safebrowsing option enabled, downloads the xml files without the message warning. The principle should be the same for Selenium with any programming language.

##### Edited: 13-04-2017

In latest version of Google Chrome the above solution is not enough. Additionally, it is necessary to start the browser with the following switch:

--safebrowsing-disable-download-protection

Now, the code for starting the browser would look something like this:

b = Watir::Browser.new :chrome, :prefs => prefs, :switches => %w[--safebrowsing-disable-download-protection]))
查看更多
登录 后发表回答