Skip to content

Instantly share code, notes, and snippets.

@gyaresu
Created April 27, 2018 00:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gyaresu/ef7b906e333a955370e4915a7f7646a4 to your computer and use it in GitHub Desktop.
Save gyaresu/ef7b906e333a955370e4915a7f7646a4 to your computer and use it in GitHub Desktop.
Downloading a file with Selenium and Python
"""
Automatically downloading Excel file from a form.
Initially because accessing __doPostBack function was too hard to script in Python.
Software installed using conda and homebrew.
- selenium from conda
- geckodriver from homebrew
"""
import os
from selenium import webdriver
def main():
""" Setup Firefox profile preferences and go get em"""
profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.folderList', 2)
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference('browser.download.dir', os.getcwd())
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/xls')
driver = webdriver.Firefox(profile)
url = 'https://example.com/some_file.aspx'
driver.get(url)
driver.find_element_by_name("btnExportToExcel").click()
driver.quit()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment