为什么硒下载不起作用?

时间:2017-09-15 05:54:36

标签: python selenium selenium-webdriver

使用selenium我尝试下载一些东西(为了验证其内容),使用以下代码作为概念验证:

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)

但是,没有“发生”(即不执行下载),也没有显示错误消息。当我手动点击该下载链接时,测试文件将被下载到/tmp

我有什么遗失的吗?

1 个答案:

答案 0 :(得分:1)

问题可能是因为这种情况下的点击需要通过子元素

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

但是,当你点击一个链接浏览器检查该链接的后台内容时,该网站似乎有问题,当我在浏览器中打开目标链接时,我得到一个空响应

Empty response