Download File using Selenium Python

Profile picture for user arilio666

To download a file in Python using Selenium, use the urllib.request.urlretrieve() method to get the file from the supplied URL. 


from selenium import webdriver
from import ChromeDriverManager
from import By
from import Service
import urllib.request

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))

downloadUrl = driver.find_element(By.ID,"download")
urllib.request.urlretrieve(downloadUrl, "luffy.csv")
  • We create a web driver instance with the Chrome driver in this code line.
  • Then, using the web driver instance's get() method, we travel to the URL where the file is located.
  • Using the webdriver instance's current_url field, we then retrieve the URL of the file to be downloaded.
  • Finally, we utilize the urllib.request module's urlretrieve() method to download the file from the specified URL and store it as "luffy.csv" in the current working directory.