How to Download Xml Files Avoiding the Popup This Type of File May Harm Your Computer Through Chromedriver and Chrome Using Selenium in Python

How to download XML files avoiding the popup This type of file may harm your computer through ChromeDriver and Chrome using Selenium in Python

Some more information about webpage from where you are trying to download the xml file might have been helpful to debug the issue of the popup with text as "This type of file may harm your computer in a better way.

However here is a sample program to download xml file from this webpage:

  • Code Block:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC

    prefs = {
    'download.default_directory': 'C:/Utility/Downloads/',
    'download.prompt_for_download': False,
    'download.extensions_to_open': 'xml',
    'safebrowsing.enabled': True
    }
    options = webdriver.ChromeOptions()
    options.add_experimental_option('prefs',prefs)
    options.add_argument("start-maximized")
    # options.add_argument("disable-infobars")
    options.add_argument("--disable-extensions")
    options.add_argument("--safebrowsing-disable-download-protection")
    options.add_argument("safebrowsing-disable-extension-blacklist")
    driver = webdriver.Chrome(options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get("http://www.landxmlproject.org/file-cabinet")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='MntnRoad.xml']//following::span[1]//a[text()='Download']"))).click()
  • Browser Snapshot:

xml_download

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

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]))

How to hide the warning This type of file can harm your computer while downloading .xml file using Chrome Chromedriver 79 with Selenium Java

To enable downloading of file using Chrome/ChromeDriver hiding the warning This type of file can harm your computer you need to:

  • Add the preferences:
    • download.default_directory
    • download.prompt_for_download
    • download.extensions_to_open
    • safebrowsing.enabled
  • As well as add the following arguments to whilelist:
    • --safebrowsing-disable-download-protection
    • safebrowsing-disable-extension-blacklist


Demonstration

To demonstrate downloading using selenium-chromedriver, and google-chrome through java I have clicked on the first Download link in the webpage http://www.landxmlproject.org/file-cabinet and your effective solution will be:

  • Code Block:

    System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
    Map<String, Object> prefs = new HashMap<String, Object>();
    prefs.put("download.default_directory", "C:/Utility/Downloads/");
    prefs.put("download.prompt_for_download", false);
    prefs.put("download.extensions_to_open", "application/xml");
    prefs.put("safebrowsing.enabled", true);
    ChromeOptions options = new ChromeOptions();
    options.setExperimentalOption("prefs", prefs);
    options.addArguments("start-maximized");
    options.addArguments("--safebrowsing-disable-download-protection");
    options.addArguments("safebrowsing-disable-extension-blacklist");
    WebDriver driver = new ChromeDriver(options);
    driver.get("http://www.landxmlproject.org/file-cabinet");
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='MntnRoad.xml']//following::span[1]//a[text()='Download']"))).click();
  • Browser Snapshot:

Java_Chrome_Download



Related Topics



Leave a reply



Submit