下面是我如何使用Firefox
webdriver下載文件。它本質上是創建一個瀏覽器配置文件,以便設置某些文件類型的默認下載位置。然後您可以驗證文件是否存在於該位置。
import os
from selenium import webdriver
browser_profile = webdriver.FirefoxProfile()
# add the file_formats to download
file_formats = ','.join(["text/plain",
"application/pdf",
"application/x-pdf",
"application/force-download"])
preferences = {
"browser.download.folderList": 2,
"browser.download.manager.showWhenStarting": False,
"browser.download.dir": os.getcwd(), # will download to current directory
"browser.download.alertOnEXEOpen": False,
"browser.helperApps.neverAsk.saveToDisk": file_formats,
"browser.download.manager.focusWhenStarting": False,
"browser.helperApps.alwaysAsk.force": False,
"browser.download.manager.showAlertOnComplete": False,
"browser.download.manager.useWindow": False,
"services.sync.prefs.sync.browser.download.manager.showWhenStarting": False,
"pdfjs.disabled": True
}
for pref, val in preferences.items():
browser_profile.set_preference(pref, val)
browser_binary = webdriver.firefox.firefox_binary.FirefoxBinary()
browser = webdriver.Firefox(firefox_binary=browser_binary,
firefox_profile=browser_profile)
# set the file name that will be saved as when you download is complete
file_name = 'ABC.txt'
# goto the link to download the file from it will be automatically
# downloaded to the current directory
file_url = 'http://yourfiledownloadurl.com'
browser.get(file_url)
# verify if the expected file name exists in the current directory
path = os.path.join(os.getcwd(), file_name)
assert os.path.isfile(path)
謝謝基蘭。這對你來說是非常慷慨的分享代碼。我意識到自己的帖子並不清楚,我很難找出正在下載文件的URL,這是呈現我的困難的一部分。是否可以修改你的代碼來選擇我想要點擊的文本來下載文件? – helloB
@helloB您可以用'browser.find_element_by_xpath(xpath).click()'替換'browser.get',它仍然會將它下載到默認目錄。 –
真棒,要試試這個,謝謝你Kiran! – helloB