Downloading a file at a specified location through python and selenium using Chrome driver.


We can download a file at a specified location through Python and Selenium using the chromedriver. We shall use the ChromeOptions class for this purpose. First, we shall create an object of the ChromeOptions class.

Then apply the add_experimental_option method on the object created. We shall pass browser preferences and download.default_directory:<location of downloaded file> as parameters to that method. Finally, this information shall be passed to the driver object.

Syntax

op = webdriver.ChromeOptions()
p = {'download.default_directory':'C:\Users\ghs6kor\Downloads\Test'}
op.add_experimental_option('prefs', p)

Example

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
#object of ChromeOptions class
op = webdriver.ChromeOptions()
#browser preferences
p = {'download.default_directory':'C:\Users\ghs6kor\Downloads\Test'}
#add options to browser
op.add_experimental_option('prefs', p)
#set chromedriver.exe path
driver = webdriver.Chrome(executable_path="C:\chromedriver.exe",
options=op)
#maximize browser
driver.maximize_window()
#launch URL
driver.get("https://www.seleniumhq.org/download/");
#click download link
l = driver.find_element_by_link_text("32 bit Windows IE")
l.click()

Output

Also, the file gets downloaded at the desired location.

Updated on: 30-Jan-2021

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements