Python Selenium Download File To Folder

Drivers download Selenium download pdf links python


Python is a popular programming language used in many applications, including web automation. Selenium is a web automation tool that allows developers to control web browsers through code. In this article, we will be discussing how to download files using Python Selenium and save them to a specific folder.


Before we begin, ensure that you have installed Selenium and the web driver for your preferred browser. You can do this by running the following commands in your terminal: “` pip install selenium “` Next, download the web driver for your browser. You can find the links to download the web drivers for different browsers in the Selenium documentation.

Downloading a File

To download a file using Selenium, we need to locate the download link on the web page and simulate a click. We can do this using the `find_element_by` method and the `click()` method. “` download_link = driver.find_element_by_link_text(‘Download’) “` Once the download link is clicked, most browsers will prompt the user to save the file. To handle the download, we need to set the browser preferences to save the file to a specific folder.

Setting Browser Preferences

To set the browser preferences, we need to create a dictionary of preferences and pass it to the `webdriver.ChromeOptions()` method. “` prefs = {‘download.default_directory’: ‘/path/to/folder’} options = webdriver.ChromeOptions() options.add_experimental_option(‘prefs’, prefs) driver = webdriver.Chrome(options=options) “` The `download.default_directory` key specifies the path to the folder where the file should be saved. Note that the path should be an absolute path.

Handling File Names

When downloading files, the browser may automatically rename the file or append a number to the file name. To handle this, we can use the `os` module to rename the file after it has been downloaded. “` import os # wait for the file to download time.sleep(5) # get the latest downloaded file list_of_files = glob.glob(‘/path/to/folder/*’) latest_file = max(list_of_files, key=os.path.getctime) # rename the file os.rename(latest_file, ‘/path/to/folder/new_file_name’) “` The `glob.glob()` method returns a list of files in the specified folder. We can then use the `max()` method and the `os.path.getctime()` method to get the latest downloaded file. Finally, we can use the `os.rename()` method to rename the file.


In this article, we have learned how to download files using Python Selenium and save them to a specific folder. We have also discussed how to set browser preferences and handle file names. With this knowledge, you can automate file downloads and streamline your web automation workflows.