File Downloads using Selenium


File Download

We cannot simulate OS actions with Selenium , for that we use AutoIt tool to upload documents (when it is not possible to upload using sendKeys method).

To handle Downloads with selenium, we need to define settings to the browser using preferences, so that it automatically downloads the files to the specified folder. Then we can write code to check if the file is downloaded or not.

If you want to download and save it to the desired location using Selenium Webdriver, then we need to set below Chrome profile preferences because we are using chrome browser.
preferences = {"download.default_directory": downloadpath }

Scenario 1 :
Open Chrome browser.
Navigate to the  https://www.python.org/ application.
Download python software from download menu.

script: 
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.action_chains import ActionChains
import time
import datetime
# Chorme Browser Launching
chromePath="C:\\BrowserDrivers\\chromedriver.exe"
downloadpath="C:\\Ddrive\\Temp"
chrome_options = webdriver.ChromeOptions()
preferences = {
"download.default_directory": downloadpath ,
              
"directory_upgrade": True,
              
"safebrowsing.enabled": True }

chrome_options.add_experimental_option(
"prefs", preferences)
driver=webdriver.Chrome(chrome_options=chrome_options,executable_path=chromePath)
driver.get("https://www.python.org/")
action=ActionChains(driver)
ele=driver.find_element_by_xpath(
"//li[@id='downloads']//a[text()='Downloads']")
action.move_to_element(ele)
action.perform()
driver.find_element_by_xpath(
"//div[@class='download-os-windows']//a[@class='button' and text()='Python 3.7.4']").click()

After executing the above code, check your Temp folder under D drive and verify that the Python Software was successfully downloaded there.

Note:
By default, ChromeDriver will create a new temporary profile for each session. At times you may want to set special preferences or just use a custom profile altogether. You can create an instance of ChromeOptions, which has convenient methods for setting ChromeDriver-specific capabilities. You can then pass the ChromeOptions object into the ChromeDriver constructor.

“download.default_directory”: downloadpath
Every browse has its own default download directory for download a file, it gets downloaded into default download directory. Generally default download directory is as below: 
C:\Users\<UserName>\Downloads

If you want to change the default download directory to custom default directory (means defined by the user), You can change it through browser setting using ChromeOptions class.
Ex:
chrome_options = webdriver.ChromeOptions()
preferences = {"download.default_directory": downloadpath}
chrome_options.add_experimental_option(
"prefs", preferences)
driver=webdriver.Chrome(
chrome_options=chrome_options,executable_path=chromePath)

for more details on chrome options you can go through the below link.

Comments