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

情到浓时终转凉″ 提交于 2021-02-10 15:36:04

问题


I want to download a daily xml file from a supplier. I managed to login and click the link Accept Download to start the downloading using chromedriver.

But I get the popup "This type of file may harm your computer". The MIME of the page is text/html, I am not sure if link is text/javascript

I tried all suggested solutions with options like

print('Starting..')
prefs = {
'download.default_directory': 'C:\\Users\MainDesk\Downloads',
'download.prompt_for_download': False,
'download.extensions_to_open': 'xml',
'safebrowsing.enabled': False
}

options = Options()

options.add_experimental_option('prefs',prefs)

browser = webdriver.Chrome(options=options, executable_path='C:\\chromedriver.exe')

How can I have my file automatically?

Additionaly I tried go in Settings of Chrome and turn off Ask to save file

I am running the script on Windows 7, with Python 3.7 and Visual Studio and the latest version of chromedriver

It is impossible to automate my download?


回答1:


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:



来源:https://stackoverflow.com/questions/55963842/this-type-of-file-can-harm-your-computer-trying-to-download-an-ini-file-in-chr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!