Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.7k points)

I am working on python and selenium. I want to download a file from clicking events using selenium. I wrote the following 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()

I want to download both files from links with the name "Export Data" from the given URL. How can I achieve it as it works with a click event only?

1 Answer

0 votes
by (62.9k points)

You have to find the link using 

find_element(s)_by_*,

 then make a call to click method.

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()

Added profile manipulation code to prevent download dialog.

If you are interested to learn Selenium on a much deeper level and want to become a professional in the testing domain, check out Intellipaat’s Selenium training course!

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

30.5k questions

32.5k answers

500 comments

108k users

Browse Categories

...