Created
April 27, 2018 00:42
-
-
Save gyaresu/ef7b906e333a955370e4915a7f7646a4 to your computer and use it in GitHub Desktop.
Downloading a file with Selenium and Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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