How to set the default download folder for web scraping using Selenium's Chrome driver.
sample.py
from selenium import webdriver
chromeOptions = webdriver.ChromeOptions()
prefs = {"download.default_directory" : "C:\Users\\{username}\\Downloads\\test"}
chromeOptions.add_experimental_option("prefs",prefs)
#Chrome diriver path
chromedriver = "/tools/chromedriver_win32/chromedriver.exe"
driver = webdriver.Chrome(executable_path=chromedriver, chrome_options=chromeOptions)
When setting the path to download.default_directory
, use\\
as the delimiter of the hierarchy, or add r
or R
, which means RAW, before the character string. ʻEx (r "C: \ Users \ {username} \ Downloads \ test") `
At this time, the characters in the root hierarchy of the drive will not work unless one delimiter is set to ʻex (C: ) . The chromedriver path should be separated by slashes (
/) and pass ʻexecutable_path
and chrome_options
as arguments when instantiating the webdriver.
Even if the chromedriver path is set in the environment variable, it seems that ʻexecutable_path is required when specifying
chrome_options`.
When you run it and Chrome starts up, check if the download folder specified in "Download save destination:" is set from [Google Chrome settings]> [Settings]> [Show advanced settings] on the upper right button. I can.