本文介绍了如何下载XML文件以避免弹出窗口这种类型的文件可能会通过ChromeDriver和Chrome(使用Python中的Selenium)损坏您的计算机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从供应商那里下载每日xml文件.我设法登录并单击链接接受下载以开始使用chromedriver下载.

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.

但是我得到弹出式窗口此类型的文件可能会损害您的计算机".页面的MIME是text/html,我不确定链接是否是text/javascript

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

如何自动保存文件?

此外,我尝试在Chrome的设置"中关闭要求保存文件"

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

我正在Windows 7,Python 3.7和Visual Studio以及最新版本的chromedriver上运行脚本

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?

推荐答案

有关网页的更多信息,您尝试从这些地方下载 xml 文件可能有助于调试带有以下文字的弹出窗口问题:""此类文件可能会以更好的方式损害您的计算机.

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文件以避免弹出窗口这种类型的文件可能会通过ChromeDriver和Chrome(使用Python中的Selenium)损坏您的计算机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 10-28 22:21