Why does the selenium download not work?

python selenium selenium-webdriver

190 观看

1回复

16014 作者的声誉

With selenium I try to download something (in order to verify its content), using the following code as a proof-of-concept:

from selenium import webdriver

profile = webdriver.FirefoxProfile()
#Set Location to store files after downloading.
profile.set_preference("browser.download.dir", "/tmp")
profile.set_preference("browser.download.folderList", 2)

#Set Preference to not show file download confirmation dialogue using MIME types Of different file extension types.
#profile.set_preference("browser.helperApps.neverAsk.saveToDisk",
#                      "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;")
profile.set_preference("browser.download.manager.showWhenStarting", False )
profile.set_preference("pdfjs.disabled", True )
profile.set_preference("browser.helperApps.neverAsk.saveToDisk","application/zip")
profile.set_preference("plugin.disable_full_page_plugin_for_types", "application/zip")

browser = webdriver.Firefox(profile)
browser.implicitly_wait(10)
browser.get('https://www.thinkbroadband.com/download')
time.sleep(15)
elem = browser.find_element_by_xpath('//a[@href="http://ipv4.download.thinkbroadband.com/5MB.zip"]')
elem.click()
time.sleep(15)

However, nothing 'happens' (i.e. the download is not performed), and also no error message is shown. When I click on that download link manually, the test-file is being downloaded into /tmp.

Is there anything I am missing?

作者: Alex 的来源 发布者: 2017 年 9 月 15 日

回应 1


1

100837 作者的声誉

决定

The issue could be because the click in this case needs to go through a child element

elem = browser.find_element_by_xpath('//a[@href="http://ipv4.download.thinkbroadband.com/5MB.zip"]/img')
elem.click()

But otherwise when you click on a link browser checks stuff in background for that link, and the site seems to have a problem, when I open the target link in browser I get an empty response

Empty response

作者: Tarun Lalwani 发布者: 2017 年 9 月 15 日
32x32