简体   繁体   中英

Loop through downloading files using selenium in Python

This is a follow-up question to this previous question on how to download ~1000 files from Google Patents.

I would like to iterate through a list of filenames fname = ["ipg150106.zip", "ipg150113.zip"] and simulate clicking and saving these files to my computer. The following example works for me and downloads a single file:

from selenium import webdriver 
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile

# Define parameters
savepath = 'D:\\' # set the desired path here for the files


# Download the files from Google Patents
profile = FirefoxProfile ()
profile.set_preference("browser.download.panel.shown", False) 

profile.set_preference("browser.download.folderList", 2) # 2 means specify custom location
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", savepath) # choose folder to download to
profile.set_preference("browser.helperApps.neverAsk.saveToDisk",'application/octet-stream')

driver = webdriver.Firefox(firefox_profile=profile)

url = 'https://www.google.com/googlebooks/uspto-patents-grants-text.html#2015'
driver.get(url)

filename = driver.find_element_by_xpath('//a[contains(text(), "ipg150106.zip")]')
filename.click()

I've tried to replace this with a list and a loop like this:

fname = ["ipg150106.zip", "ipg150113.zip"]

for f in fname:
    filename = driver.find_element_by_xpath('//a[contains(text(), f)]')
    filename.click()
    print('Finished loop for: {}.'.format(f))

However, the browser opens, but nothing happens (no clicking on files). Any ideas?

You need to pass the filename into the XPath expression:

filename = driver.find_element_by_xpath('//a[contains(text(), "{filename}")]'.format(filename=f))

Though, an easier location technique here would be "by partial link text" :

for f in fname:
    filename = driver.find_element_by_partial_link_text(f)
    filename.click()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM