Need to download a file using Selenium

0 votes

I'm working on Selenium with python and I want to download a file by "click event" using selenium. Below is the code and site. Can someone plz tell me how to complete this code to download both files with name "Export Data" from above url via my code?

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys

browser = webdriver.Firefox()
browser.get("http://www.drugcite.com/?q=ACTIMMUNE")

browser.close()

Thanks

Mar 30, 2018 in Selenium by Shubham
• 13,490 points
3,349 views

1 answer to this question.

0 votes

Pretty simple...Find the element or link using find_element_by_*, function and then call the click() method. Code is below. Note that I have also added browser profiling in my code to avoid the download dialog from popping up.

from selenium import webdriver

# To prevent download dialog
profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.folderList', 2) # custom location
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference('browser.download.dir', '/tmp')
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'text/csv')

browser = webdriver.Firefox(profile)
browser.get("http://www.drugcite.com/?q=ACTIMMUNE")

browser.find_element_by_id('exportpt').click()
browser.find_element_by_id('exporthlgt').click()
answered Mar 30, 2018 by nsv999
• 5,500 points

Related Questions In Selenium

+1 vote
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer