Set default download folder for Selenium Chrome driver

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)

important point

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`.

Verification

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.

Recommended Posts

Set default download folder for Selenium Chrome driver
Overwrite download file for python selenium Chrome
How to automatically install Chrome Driver for Chrome version with Python + Selenium + Chrome
Download PDF with selenium chromedriver (Chrome 57) + (Other notes)