I recently had the idea to use Selenium with Python to automate some repetitive tasks in SAP for a client. And as always happens when you get your hands dirty with the code, I started to find some challenges that I never saw coming. After having spent a long time browsing the Internet trying to find the most suitable solution for each problem, thought:
How good would it have been if you had found everything you needed gathered in one place, ready to use?
Then, to make their lives easier, I gathered in this article, the answers to the most frequent challenges a user can find by using Selenium in conjunction with Out-of-the-box code snippets written in Python.
PD: If you still don't feel at home with the basics of Selenium, You can check out our Getting Started Guide to Selenium. Article first.
And now let's jump into action!
How to download files using Selenium WebDriver
When using Selenium to automate browsing, you may need to download files. The problem is that, as soon as we select and click the download link, a native dialog window appears requiring manual intervention. Selenium has no control over your browser's file download windows, cannot continue with the download process. Fortunately, there are always alternatives.
To solve this problem, you can authorize your browser to automatically download files beforehand and set a default download file location. This can be done by setting the preferences for the WebDriver profile (the code below is for a Firefox browser, you may need to adapt it to work in other browsers):
import download_dir = os.getcwd()# current working directory profile = webdriver.FirefoxProfile() profile.set_preference("browser.download.folderList", 2) # the custom location specified browser.download.dir is enabled when browser.download.folderList equals 2 profile.set_preference('browser.download.dir', download_dir) profile.set_preference("browser.download.manager.showWhenStarting", False) profile.set_preference("browser.download.dir", path) profile.set_preference('browser.helperApps.neverAsk.saveToDisk', CONTENT_TYPE) profile.set_preference("webdriver_enable_native_events", False) profile.set_preference("browser.download.manager.scanWhenDone",False) profile.set_preference("browser.download.manager.useWindow",False) profile.set_preference("browser.helperApps.alwaysAsk.force",False) profile.update_preferences() browser = webdriver.Firefox(firefox_profile=profile)
You may have noticed the CONTENT_TYPE
variable you need to replace. In reality, corresponds to the MIME type of the file you are downloading. If you are not familiar with this term, Mime Type it is simply an identifier that is used to recognize a type of data for content on the Internet. It serves the same purpose on the Internet as file extensions in your operating system.
To simplify your task, I removed the MIME types of the most common extensions from Mozilla web documents, to facilitate access. In the widget below, you will find the code you need to use to get the content type corresponding to your file extension. You can use the console to test it immediately!!
You can get the JSON file by copying and pasting from the widget above or downloading it from Github.
Now, your WebDriver should be able to download without asking for permission or showing dialog windows 😉